Systems and Methods for Performing Concurrency Restriction and Throttling over Contended Locks

ABSTRACT

A concurrency-restricting lock may divide a set of threads waiting to acquire the lock into an active circulating set (ACS) that contends for the lock, and a passive set (PS) that awaits an opportunity to contend for the lock. The lock, which may include multiple constituent lock types, lists, or queues, may be unfair over the short term, but improve throughput of the underlying multithreaded application. Culling and long-term fairness policies may be applied to the lock to move excess threads from the ACS to the PS or promote threads from the PS to the ACS. These policies may constraint the size or distribution of threads in the ACS (which may be NUMA-aware). A waiting policy may avoid aggressive promotion from the PS to the ACS, and a short-term fairness policy may move a thread from the tail of a list or queue to its head.

FIELD OF THE DISCLOSURE

This application is a continuation of U.S. patent application Ser. No.14/818,213, filed Aug. 4, 2015, which is hereby incorporated byreference herein in its entirety.

BACKGROUND

This disclosure relates generally to managing accesses to sharedresources in a multithreaded environment, and more particularly tosystems and methods for performing concurrency restriction andthrottling over contended locks.

Description of the Related Art

In a multiprocessor environment with threads and preemptive scheduling,threads can participate in a mutual exclusion protocol through the useof lock or “mutex” constructs. A mutual exclusion lock can either be ina locked state or an unlocked state, and only one thread can hold or ownthe lock at any given time. The thread that owns the lock is permittedto enter a critical section of code protected by the lock or otherwiseaccess a shared resource protected by the lock. If a second threadattempts to obtain ownership of a lock while the lock is held by a firstthread, the second thread will not be permitted to proceed into thecritical section of code (or access the shared resource) until the firstthread releases the lock and the second thread successfully claimsownership of the lock.

In modern multicore environments, it can often be the case that thereare a large number of active threads, all contending for access to ashared resource. As multicore applications mature, situations in whichthere are too many threads for the available hardware resources toaccommodate are becoming more common. This can been seen incomponent-based applications with thread pools, for example. Often,access to such components are controlled by contended locks. As threadsare added, even if the thread count remains below the number of logicalCPUs, the application can reach a point at which aggregate throughputdrops. In this case, if throughput for the application is plotted on they-axis and the number of threads is plotted on the x-axis, there will bean inflection point beyond which the plot becomes a concave plot. Pastinflection point, the application encounters “scaling collapse” suchthat, as threads are added, performance drops. In modern layeredcomponent-based environments it can difficult to determine a reasonablelimit on the thread count, particularly when mutually-unaware componentsare assembled to form an application.

Current trends in multicore architecture design imply that in comingyears, there will be an accelerated shift away from simple bus-baseddesigns towards distributed non-uniform memory-access (NUMA) andcache-coherent NUMA (CC-NUMA) architectures. Under NUMA, the memoryaccess time for any given access depends on the location of the accessedmemory relative to the processor. Such architectures typically consistof collections of computing cores with fast local memory (as found on asingle multicore chip), communicating with each other via a slower(inter-chip) communication medium. In such systems, the processor cantypically access its own local memory, such as its own cache memory,faster than non-local memory. In some systems, the non-local memory mayinclude one or more banks of memory shared between processors and/ormemory that is local to another processor. Access by a core to its localmemory, and in particular to a shared local cache, can be several timesfaster than access to a remote memory (e.g., one located on anotherchip). Note that in various descriptions herein, the term “NUMA” may beused fairly broadly. For example, it may be used to refer to non-uniformcommunication access (NUCA) machines that exhibit NUMA properties, aswell as other types of NUMA and/or CC-NUMA machines.

On large cache-coherent systems with Non-Uniform Memory Access (CC-NUMA,sometimes shortened to just NUMA), if lock ownership migrates frequentlybetween threads executing on different nodes, the executing program cansuffer from excessive coherence traffic, and, in turn, poor scalabilityand performance. Furthermore, this behavior can degrade the performanceof other unrelated programs executing in the system.

SUMMARY

A concurrency-restricting lock may divide a set of threads waiting toacquire the lock into two sets: an active circulating set (ACS) that iscurrently able to contend for the lock, and a passive set (PS) thatawaits an opportunity to be able to contend for the lock (e.g., byjoining the active circulation set). For example, the ACS may includethe current lock owner, threads that are waiting to acquire the lock(e.g., one thread or a small number of threads), and/or threads that arecurrently executing their non-critical sections (e.g., one thread or asmall number of threads that may attempt to acquire the lock when theyreach their critical sections). In some embodiments, each of the threadsin the ACS may circulate from executing its non-critical section towaiting, from waiting to lock ownership and execution of its criticalsection, and then back to executing its non-critical section. Variousadmission policies, some of which are NUMA-aware, may place arrivingthreads in one of the two sets randomly, on a first-come-first servedbasis, or using other criteria, in different embodiments.

The concurrency-restricting lock may include multiple constituent locktypes, lists, or queues, in some embodiments. For example, in variousembodiments, the concurrency-restricting lock may include an inner lockand an outer lock of different lock types, a main stack (or queue)representing the ACS and an excess list representing the PS, and/or asingle stack (or queue), portions of which represent the ACS and PS(with or without an additional list of excess or remote threads). Theconcurrency-restricting lock may be unfair over the short term, but mayimprove the overall throughput of the underlying multithreadedapplication through passivation of a portion of the waiting threads, andvarious techniques for managing the intermixing of threads from the ACSand PS, the selection of a successor for lock ownership, and the handoffbetween the lock owner and its successor.

In some embodiments, a culling policy may be applied to theconcurrency-restricting lock to move excess threads from the ACS to thePS. The culling policy may limit the size and/or distribution of threadsin the ACS (which may be NUMA-aware). In some embodiments, a long-termfairness policy may be applied to the concurrency-restricting lock topromote threads from the PS to the ACS. The long-term fairness policymay also constrain the size and/or distribution of threads in the ACS(especially in embodiments in which the concurrency-restricting lock isNUMA-aware).

In some embodiments (e.g., in those in which the ACS is represented byan unfair stack or queue, such as one that implements LIFO ordering), ashort-term fairness policy may, from time to time, move a thread fromthe tail of the stack or queue to the head of the stack or queue. Insome embodiments (e.g., embodiments in which the lock ownershipsuccession policy has a preference for threads already in the ACS,rather than threads in the PS), a waiting policy (such as an“anti-spinning” approach) may avoid aggressive promotion from the PS tothe ACS.

Several specific, but non-limiting, examples of concurrency-restrictinglocks (some of which are NUMA-aware) are described in detail herein,including an outer-inner dual path lock, and concurrency-restrictinglocks that have been constructed through the transformation of variousMCS type locks and LIFO structures.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 is a block diagram illustrating one embodiment of aconcurrency-restricting lock, as described herein.

FIG. 2 is a flow diagram illustrating one embodiment of a method forrestricting concurrency on a contended lock, as described herein.

FIG. 3 is a block diagram illustrating a portion of a computer systemthat implements a NUMA style memory architecture, according to someembodiments.

FIGS. 4A and 4B are block diagrams illustrating one embodiment of anouter-inner dual path lock.

FIG. 5 is a flow diagram illustrating one embodiment of a method foracquiring an outer-inner dual path lock, as described herein.

FIG. 6 is a flow diagram illustrating one embodiment of a method forculling an active circulation set of a concurrency-restricting lock, asdescribed herein.

FIG. 7 is a flow diagram illustrating one embodiment of a method forreleasing a concurrency-restricting lock, as described herein.

FIG. 8 is a block diagram illustrating one embodiment of NUMA-awarelast-in-first-out type lock, as described herein.

FIG. 9 is a flow diagram illustrating one embodiment of a method forreleasing a NUMA-aware LIFO lock, as described herein.

FIG. 10 illustrates a computing system configured to implement some orall of the methods described herein for restricting concurrency oncontended locks, according to various embodiments.

While the disclosure is described herein by way of example for severalembodiments and illustrative drawings, those skilled in the art willrecognize that the disclosure is not limited to embodiments or drawingsdescribed. It should be understood that the drawings and detaileddescription hereto are not intended to limit the disclosure to theparticular form disclosed, but on the contrary, the disclosure is tocover all modifications, equivalents and alternatives falling within thespirit and scope as defined by the appended claims. Any headings usedherein are for organizational purposes only and are not meant to limitthe scope of the description or the claims. As used herein, the word“may” is used in a permissive sense (i.e., meaning having the potentialto) rather than the mandatory sense (i.e. meaning must). Similarly, thewords “include”, “including”, and “includes” mean including, but notlimited to.

DETAILED DESCRIPTION OF EMBODIMENTS

As noted above, as multicore applications mature, developers may facesituations in which there are too many threads for the availablehardware resources to be able to handle effectively and/or fairly. Forexample, this may been the case in component-based applications thatemploy thread pools. Often, such components have contended locks. Insome embodiments of the systems described herein,concurrency-restricting modifications to those locks may be leveraged torestrict the number of threads in circulation, thus reducing destructiveinterference (e.g., in last-level shared caches, or for other types ofshared resources).

For example, in some cases, a contended lock that protects and/orcontrols access to a critical section of code (CS) or shared resourcemay have an excessive number of threads circulating through thecontended lock. In this context, the term “excessive” may refer to asituation in which there are more than enough threads circulating overthe lock to keep the lock fully saturated. In such situations, theexcess or surplus threads typically do not contribute to performance,and often degrade overall collective throughput. In some embodiments, inorder to reduce interference and improve performance, the systemsdescribed herein may apply selective culling and/or passivation of someof the threads circulating over the lock. While these techniques may beconsidered palliative, in practice, they may be effective in a varietyof different contexts and applications, in different embodiments. Inaddition, in scenarios in which they are ineffective, these techniquesmay at least do no harm (e.g., they may not negatively affectperformance or resource sharing).

As used herein, the term “lock working set” (LWS) may refer to the setof threads that circulates over a lock during some interval. In variousembodiments, the techniques described herein may strive to minimize theLWS size over short intervals while still keeping the lock fullysaturated and subscribed. In some embodiments, this may be accomplishedby partitioning the circulating threads into an “active circulation set”(sometimes referred to herein as the ACS) and a “passive set” (sometimesreferred to as the PS). The techniques described herein may act tominimize the size of the ACS while still remaining work conserving. Forexample, these techniques may be used to ensure that the ACS issufficiently large to saturate the lock (so that the lock is notunnecessarily under-provisioned), but no larger. By restricting andconstraining the size of the ACS, the number of threads circulating overthe lock in a given interval may be reduced.

In some embodiments, the lock subsystems described herein may deactivateand/or quiesce threads in the passive set through culling operationsthat act to minimize the size of the ACS. Under fixed load, aggressiveculling may cause the system to devolve to a state where there is atmost one member of the ACS waiting to acquire the lock, while otherthreads wait in the PS for an opportunity to contend for the lock. Inthis state, the ACS may include that one waiting thread, the currentowner of the lock (which may be executing a critical section of codeand/or accessing a shared resource that is protected by the lock), and anumber of threads that are circulating through their respectivenon-critical sections. The waiting thread may typically acquire the lockafter the lock owner releases it. Subsequently, some other member of theACS may complete its non-critical section (NCS) and begin waiting forthe lock, as in a classic birth-death renewal process. In someembodiments, the admission order may effectively be first-in-first-out(FIFO) ordering over the members of the ACS, regardless of theprevailing lock admission policies. In some such embodiments, theadmission order schedule for the members of the ACS may be moreprecisely described as being round-robin cyclic.

In some embodiments that employ the concurrency-restricting mechanismdescribed herein, threads in the ACS may have to busy-wait only brieflybefore acquiring a contended lock. In some embodiments, at most onethread in the ACS may be waiting to acquire a contended lock at anygiven moment. As described in more detail herein, excess threads may bequarantined in the PS and may be blocked in the kernel. In variousembodiments, threads in the ACS may be thought of as being “enabled” andmay operate normally, while threads in the PS may be thought of as being“disabled” and may not circulate over the lock. As described in moredetail below, threads may, from time to time, be explicitly shiftedbetween the active circulation set and the passive set (e.g., to ensurelong-term fairness). In various embodiments, the techniques describedherein may be used to constrain concurrency in order to protectresources (e.g., residency in shared caches). These technique may beunfair over the short-term, but may increase throughput.

The issues that may be addressed through the use of theconcurrency-restricting techniques described herein may be illustratedby way of the following example. Assume that the threads of amultithreaded application loop through the following operations: 1)acquire a central lock; 2) execute a critical section of code that isprotected by the central lock; 3) release the central lock; 4) execute anon-critical section of code. By Amdahl's law or Gustafson'sreformulation, if the critical section length is C and the non-criticalsection period is N, then under ideal circumstances and scheduling, themaximum number of threads that can “fit” in the lock schedule beforeencountering contention (triggering the need for at least some threadsto wait) may be calculated as (N+C)/C. In this ideal model, thetechniques described herein may be used to hold the size of the ACS atapproximately (N+C)/L (or slightly above). In this case, the lock may beconsidered to be fully saturated or to have a sufficient quorum. Inpractice, over-saturation may be detected by detecting contention on thelock. For example, excessive waiting on the central lock (or on an outerlock of an outer-inner dual path lock, such as those described below)may indicate that there is a surplus of threads, and that it may beappropriate to apply one or more of the concurrency restrictiontechniques described herein. Note that, in various embodiments, thesetechniques may adapt automatically and immediately to varying load. Inaddition, these techniques may be robust and require no tuning.

One embodiment of a concurrency-restricting lock is illustrated by theblock diagram in FIG. 1. In this example, a concurrency-restricting lock100 (which may, in different embodiments, protect a critical section ofcode in a multithreaded application or a shared resource accessed bymultiple threads of a multithreaded application) includes a datastructure 150 that stores data representing the lock state and/or otherdata associated with the lock (e.g., metadata representing the lockowner or preferred/home node, a pointer to a successor for lockownership, or other information), a data structure 110 representing anactive circulation set (e.g., a set of threads that circulate over thelock, contending for lock ownership), and a data structure 140representing a passive set (e.g., a set of threads that are waiting foran opportunity to contend for ownership of the lock).

As illustrated in this example, a data structure representing the activecirculation set (such as data structure 110) may maintain datarepresenting multiple threads that (as indicated by the dashed linesfrom the elements 120 a-120 d to lock state/data 150) are currentlycontending for the concurrency-restricting lock (e.g., lock 100). Asillustrated in this example, a data structure representing the passiveset (such as data structure 140) may maintain data representing multiplethreads that are not currently able to contend for ownership of the lock(e.g., at least elements 130 a-130 f). As illustrated by thebi-directional line between active circulation set 110 and passive set140, and described in detail herein, in various embodiments, threadsthat are part of an active circulation set for a concurrency-restrictinglock may be culled from the active circulation set and placed in thepassive set (thus limiting the number of threads in active circulation)and/or threads that are part of the passive set for aconcurrency-restricting lock may be promoted to the active circulationset (thus being afforded an opportunity to contend for theconcurrency-restricting lock and increasing long-term fairness withrespect to lock ownership). Various methods for determining when, andif, to apply such a culling operation or fairness policy are describedherein, according to different embodiments.

In various embodiments, as threads arrive at a shared lock, they may bepartitioned into multiple sets of threads, including an activecirculation set (which may include threads that are circulating over theshared lock) and a passive set (which may include threads that are notimmediately able to content for the shared lock, but that are awaitingan opportunity to contend for the shared lock), according to differentadmission policies. A thread in the active circulation set may, afteracquiring the shared lock, access a critical section of code or sharedresource that is protected by the shared lock, after which it mayrelease the lock. As described herein, the thread releasing the sharedlock may, in different embodiments, pass ownership of the shared lock toanother thread in the active circulation set, may cull one or morethreads from the active circulation set, and/or may promote one or morethreads from the passive set to the active circulation set, according todifferent culling policies and/or different fairness policies.

One embodiment of a method for restricting concurrency on a contendedlock is illustrated by the flow diagram in FIG. 2. As illustrated at210, in this example, the method may include a thread arriving at aconcurrency-restricting lock that protects a critical section of code orshared resource (e.g., by invoking an operation or process forattempting to acquire the lock). If it is determined that the threadshould not be placed in the active circulation set to contend for theconcurrency-restricting lock (shown as the negative exit from 215), themethod may include placing the thread in passive set of threads that arewaiting for the opportunity to contend for the concurrency-restrictinglock, as in 220. If it is determined that the thread should be placed inthe active circulation set to contend for the concurrency-restrictinglock (shown as the positive exit from 215), and if the lock is currentlyheld by another thread (shown as the positive exit from 225), the methodmay include waiting until the concurrency-restricting lock is no longerheld before attempting to acquire it. This is illustrated in FIG. 2 bythe feedback from the positive exit of 225 to its input.

As illustrated in this example, whether the thread is placed in theactive circulation set for the concurrency-restricting lock or it isdetermined that the thread should not be placed in the activecirculation set, if the lock is not currently held by another thread(shown as the negative exit from 225), the method may include one ormore of the threads in the active circulation set attempt to acquire thelock, as in 230. If the lock is acquired (shown as the positive exitfrom 235), the method may include the acquiring thread accessing thecritical section or shared resource that is protected by theconcurrency-restricting lock, and then initiating the release of theconcurrency-restricting lock (as in 240). As illustrated in thisexample, whether or not the lock is soon acquired, the method mayinclude determining whether a thread is to be pulled from the passiveset (as in 245). If so (e.g., in accordance with a long-term fairnesspolicy), the method may include the pulled thread joining the activecirculation set (as in 250). The method may also include determiningwhether one or more threads should be culled from the active circulationset (as in 255). If so (e.g., in accordance with a culling policy thatlimits the number of threads in the active circulation set and/orattempts to enforce a NUMA-aware policy for the distribution of threadsin the active circulation set and the passive set), the method mayinclude culling one or more threads from the active circulation set (asin 260).

As illustrated by the feedback from the negative exit of 265 to 230, ifno other threads have arrived at the concurrency-restricting lock in themeantime, the method may include repeating the operations illustrated inelements 230-260, as threads in the active circulation set circulateover the concurrency-restricting lock (acquiring and releasing the lockone at a time). However, when and if another thread arrives at theconcurrency-restricting lock (e.g., if another thread has arrived at theconcurrency-restricting lock while other operations illustrated in FIG.2 are taking place), the method may include repeating any or all of theoperations illustrated in FIG. 2, as appropriate, including thoseillustrated in elements 215-260. While this is illustrated in FIG. 2 bythe feedback from the positive exit of 265 to 215, in variousembodiments, another thread may arrive at the concurrency-restrictinglock any time (including, for example, while the lock is currently held,as in 225). In some embodiments, as soon as another thread arrives atthe concurrency-restricting lock, it may be placed in one of the twosets of threads associated with the lock (e.g., the active circulationset or the passive set) regardless of which of the operationsillustrated in FIG. 2 are currently being performed. In differentembodiments, the arrival of the other thread may or may not affect theperformance of any those operations that are currently in progress.

In some embodiments, the concurrency-restricting techniques describedherein may be used primarily to manage the size of the activecirculation set. For example, in some embodiments, concurrencyrestriction mechanisms may be used to cull excess threads from theactive circulation set to reduce the size of the ACS (in an attempt tocomply with a concurrency restriction policy). However, in some systems,the performance of the system may be dependent on the composition and“demographics” of the threads that make up the elements of the activecirculation set in addition to being dependent on the size of the activecirculation set. For example, in some systems, it may be desirable forthe threads in the active circulation set to be equally distributed overthe available cores in order to reduce unnecessary and futilecompetition for shared resources (e.g., pipeline resources) that couldarise from an imbalanced active circulation set. Furthermore, it may bedesirable for the ACS elements to be co-located on a single NUMA node tothe extent possible, at least for certain periods of time, while stillremaining work conserving. As described herein, this may be achieved bya NUMA-friendly concurrency-restricting lock, in some embodiments.

In various embodiments, NUMA-aware lock designs may be directedprimarily to the reduction of the lock migration frequency (e.g., so asto generate better node-local locality of reference for a lock and acritical section of code or data that it protects). NUMA-aware locks mayact to reduce the rate of write invalidations and coherence missessatisfied by cache-to-cache transfers from remote caches via theinterconnect. In some embodiments, reducing the lock migration rate (aswith the NUMA-aware concurrency-restricting locks described herein) maysignificantly curtail the spread of the lock metadata and criticalsection data across these nodes.

FIG. 3 is a block diagram illustrating portion of a computer system thatimplements a NUMA style memory architecture, according to oneembodiment. For example, the computer system illustrated in FIG. 3includes multiple CPU boards 300 (shown as 300 a-300 n) that communicatewith each other over interconnect 350. In this example, each of the CPUboards (which include a shared board-local memory 325) may be considereda node in the NUMA system, while in other embodiments, each node of aNUMA system may be a socket or die that includes multiple processorcores that share memory. One of these CPU boards (300 a) is illustratedin more detail than the others. In some embodiments, each of the CPUboards 300 may include the same or a similar architecture as thatillustrated for CPU board 300 a. In other embodiments, each of the CPUboards may include a different number and/or arrangement of processorcores, caches, etc. As illustrated in FIG. 3, the computer system mayalso include one or more system memories 360 and/or other components370. In this example, CPU board 300 a includes two processor chips(shown as processor chips 310 a and 310 b) that communicate with eachother and with shared memory 325 over interconnect 340. In this example,processor chips 310 a and 310 b include a similar architecture.

In the example illustrated in FIG. 3, processor chip 310 a includes fourprocessor cores (shown as 320 a-320 d), and each processor core has arespective (dedicated) level 1 (L1) cache (shown as 330 a-330 d).Similarly, processor chip 310 b includes four processor cores (shown as320 e-320 h), and each processor core has a respective (dedicated) level1 (L1) cache (shown as 330 e-330 h). Each processor core may be amulti-threaded core, in some embodiments. For example, in one embodimenteach processor core may be capable of concurrently executing eighthardware threads.

Inter-thread communication via local caches (e.g., L1 and L2 caches) maybe significantly faster than inter-thread communication via remotecaches because the latter involves cache coherence messages that arecommunicated across one or more of the interconnects 340 and 350. Forexample, the threads executing on a given processor core 320 may sharethe level 1 cache 330 for that processor core 320, and accesses to thislevel 1 cache, which may be considered local to the processor core 320and its hardware threads, may be extremely fast. In addition, the fourprocessor cores 320 of processor chip 310 a may share the level 2 (L2)cache 335 a for processor chip 310 a, and accesses to this level 2 cache(by the four processor cores of processor chip 330 a) may also be fast,although not as fast as those to each processor core's own level 1cache. Similarly, the four processor cores 320 of processor chip 310 bmay share the level 2 (L2) cache 335 b for processor chip 310 b, andaccesses to this level 2 cache (by the four processor cores of processorchip 310 b) may also be fast, although not as fast as those to eachprocessor core's own level 1 cache.

In this example, accesses to caches of a different processor chip 310 onthe same CPU board 300, to caches of a processor chip 310 on a differentCPU board 300, to a board-local shared memory 325 on a different CPUboard 300, and/or to various system memories 360 (all of which may beconsidered remote accesses with respect to a hardware thread executing aparticular processor core 320 of a processor chip on CPU board 300 a)may exhibit increasingly higher latency, when compared to accesses tothe level 1 and level 2 caches that are local to that hardware thread.

In some embodiments, threads executing in a NUMA system may executecritical sections that are protected by a concurrency-restricting lock(such as those described herein) and/or may access shared resources thatare protected by such a concurrency-restricting lock. For example, insome embodiments, NUMA-aware concurrency restriction (which as describedherein, may represent a special case of concurrency restriction) may beused to cull NUMA-distant threads from the ACS and may operate tominimize the NUMA-diversity of the ACS. In other words, while classicconcurrency restriction may be concerned only with reducing the size ofthe ACS (while still remaining work conserving), NUMA-aware concurrencyrestriction may strive to reduce or minimize lock migration by activelycontrolling the makeup (e.g., the NUMA-diversity) of the ACS and/or thenumber of inter-node transitions reflected in the ACS admission orderschedule (while still remaining work conserving). In some embodiments, aclassic “cohort” NUMA-friendly lock may employs concurrency restrictionin an attempt to restrict the ACS to one node for some period of time inorder to reduce the rate of lock migration, which in turn may yieldimproved performance. Any of a variety of NUMA-aware locks may employsome form of concurrency restriction to control and reduce lockmigration, in different embodiments. Note that, in some embodiments,both techniques may be combined. For example, in some embodiments,classic concurrency restriction may be applied to intra-node lockscheduling, while NUMA-aware concurrency restriction may be applied tointer-node lock scheduling. In at least some embodiments, the exampleMCSCRN and LIFON locks described below provide both classic andNUMA-aware types of concurrency restriction.

In various embodiments, the admission policy for a combined form ofconcurrency restriction (e.g., one that provides both classic andNUMA-aware types of concurrency restriction) may specify the following:

-   -   A. Given a sufficient number of participating threads, and for        short episodes, membership in the ACS should be restricted to        threads that originated from the currently preferred NUMA node.        In this case, the policy may act to “homogenize” the ACS with        respect to NUMA diversity. This may also act to reduce lock        migration, which in turn may improve aggregate throughput over        the lock. Note that restricting membership in the ACS to threads        that originated from the currently preferred NUMA node for        longer episodes may increase performance, but may sacrifice        short-term fairness.    -   B. Given (A) and a NUMA “homogenized” ACS, the maximum size of        the ACS should be limited to the minimum number of threads        needed to saturate the lock, and any surplus threads should be        passivated (e.g., placed in the passive set).

In addition to controlling the demographic composition of the ACS, anideal NUMA-aware concurrency-restricting lock implementation may alsostrive to minimize the number of NUMA node transition edges in the ACSadmission schedule.

It should be noted that the peak throughput of a system may appear at athread count below the lock's saturation level. In these cases,concurrency restriction (CR) techniques may provide neither harm norbenefit, as the collapse point and the saturation point are unrelated.However, peak throughput may never occur beyond (e.g., at a higherthread count than) the lock's saturation point. The scalability collapsephenomena generally involves competition for shared hardware resources.A classic example is residency in a shared cache. As more threadscirculate over a lock in unit time, more pressure is put on the cacheand miss rates increase. Critically, as the cache is shared, theresidency of the data accessed by a thread decays over time due to theaction of other concurrently running threads that share that cache. Insome embodiments of the systems described herein, by reducing the numberof threads that are circulating, cache pressure may be reduced andresidency may be retained for longer periods, thus reducing the missrate. In other words, concurrency restriction, as described herein, mayreduce destructive interference in shared caches. Note that when thisinterference results in cache misses, then there may be increasedcontention for the channels to and from main memory (e.g., from DRAMsmaking up main memory). Thus, when cache misses are reduced throughconcurrency restriction, DRAM channel congestion and competition for thebandwidth of those channels may also be reduced.

More specifically, with a sufficiently large number of threadscirculating over a contented lock, the overall throughput of anapplication may become dominated solely by the duration of the criticalsection protected by the lock. Note that, in the examples describedherein, it may be assumed that actions taken in the critical sectionexhibit locality and that the non-critical sections accessthread-private data. Furthermore, it may be presumed that criticalsection invocations under the same lock exhibit reference similarity.Therefore, acquiring lock L may be a good predictor that the criticalsection protected by L will access data that was accessed by recentprior critical sections protected by L (i.e., critical sectioninvocations tend to access data accessed by prior critical sectioninvocations). The actions taken by concurrent threads in thenon-critical section may tend to erode the last level cache (LLC)residency of the critical section data. If the ACS is large, theeviction pressure on the critical section data by multiple non-criticalsection instances may become more intense. In turn, the critical sectionmay incur more misses in the LLC, the critical section duration mayincrease, and, consequently, throughput may drop. On the other hand, byminimizing the ACS, aggregate throughput may be increased. Again notethat by minimizing the LLC miss rate, contention on the DRAM channelsmay also be reduced.

In one example, a database application in which each critical sectionoperation accesses a record in a large (e.g., 2 MB) customer databasemay execute on a single-socket processor that includes an 8 MB sharedLLC. In this example, each individual critical section may access onlyone record, but over time most records will be accessed repeatedly bysubsequent operations. In other words, the critical section itself maybe “short” in terms of its average duration but its repeated executionmay have a wide impact in the sense that a set of critical sectioninvocations will eventually touch all 2 MB in the database. In thisexample, there are 16 threads, but the (NCS+CS)/CS ratio may be suchthat only five threads are needed to fully saturate the lock andprovision the ACS. Furthermore, the non-critical section “footprint” ofeach thread is 1 MB. In this example, even though an individualnon-critical section invocation may be short, over time, a thread mayaccess all 1 MB of its thread-private data. In this example, asdescribed above, the critical section data may be shared data, while thenon-critical section data may be per-thread data that is thread-private.

In this example, under a classic FIFO MCS type lock, all 16 threads willcirculate over the lock in round-robin cyclic order. Therefore, thetotal footprint would be 18 MB, which includes 16 threads times 1MB/thread for the non-critical sections (for a total of 16 MB) plus 2 MBfor the critical section. This 18 MB footprint clearly exceeds the 8 MBcapacity of the LLC. Therefore, the non-critical section operations willerode and decay the LLC cache residency of the critical section data.That will slow down the execution of the critical section, and degradeoverall throughput. However, if the system employs the concurrencyrestriction techniques described herein, the lock subsystem may be ableto limit the size of the ACS. For example, if the size of the ACS is setat five threads, the total footprint would be 7 MB, which includes 5threads times 1 MB/thread for the non-critical sections (for a total of5 MB) plus 2 MB for the critical section. In this mode, the totalfootprint (e.g., the critical section data plus the non-critical sectiondata of the ACS threads) would fit comfortably within the LLC.Consequently, the non-critical section instances may not erode thecritical section data LLC residency, the critical section may not sufferfrom misses arising from destructive interference in the LLC, andthroughput may be improved. In other words, the techniques described indetail herein may reduce cache pressure, and more specifically pressureon the critical section data, which is often critical for throughput.

Another shared resource that may be conserved via concurrencyrestriction, such as that described herein, is per-chip thermal andenergy limits. For example, in systems that employ facility based onrunning average power limiting to cap energy, the use of concurrencyrestriction may yield better performance. In systems that are uncapped,the use of concurrency-restricting techniques may also reduce energy usewhile still providing improved performance, in that concurrencyrestriction may act to maximize the performance per joule.

In some embodiments, concurrency restriction may reduce the competitionfor logical CPUs or shared pipelines. For example, a core may have a lotof pipelined resources, and if there is only one thread running on thecore, all those resources can work together to make that one thread runvery fast. However, when there are multiple threads on the core, thehardware will start divvying up the pipeline resources more and more,and each of the threads may run more slowly. At some point, withoutconcurrency restriction, threads may be waiting around starved for acycle start (for a pipeline resource).

In some embodiments, including many of those described herein, locksaturation levels may be used to control concurrency restriction. Inother embodiments, however, it may be possible to use hardwareperformance counters to measure competition for shared resources. In oneexample, a counter that reflects the number of cycles logical CPUsstalled waiting for pipelines may provide a metric for being “cyclebound”, in which case a concurrency-restricting lock implementation mayact to more aggressively throttle concurrency. Similarly, concurrencyrestriction may be imposed when thermal throttling is active, in someembodiments. For example, a multi-core processor that allows up to 64threads may actually have a thermal bound that means it can only supportsixteen threads or fewer simultaneously. When that thermal bound isexceeded, the processor may start overheating and, recognizing that itis overheating, may throttle down the clock rate. In this example, theconcurrency restriction techniques described herein may be used to avoida situation in which the processor overheats and/or reduces the clockrate.

In some embodiments, concurrency restriction may act to reduce voluntarycontext switch rates. For example, context switches are typicallyexpensive, e.g., with latency worse than 10000 cycles. They may reflectwasted administrative work (e.g., co-ordination overhead) that does notdirectly contribute toward forward progress of the executingapplication(s). In some embodiments, since the passive set can remainstable for some period, threads in the passive set may incur lesscontext switching, which in turn means that the CPUs on which thosethreads were running may be eligible to use deeper sleep states andenjoy reduced power consumption and more thermal headroom for turbomode. Note that, generally, the deeper the sleep state, the more poweris conserved while in that state, but the longer it takes for the CPU toenter and exit that state. For example, to support a default Linuxkernel idle policy called the “ladder” scheme, a module may force CPUsinto deeper sleep states based on how long the CPU has been idle. Deepersleep states may consume significantly less power, but a CPU at a deeperstate may also take far longer to exit that state and resume normalexecution, as would happen when a thread is made ready and dispatchedonto an idle and available CPU.

In some embodiments, by minimizing the size of the ACS, the set of CPUshosting the ACS threads may be fully utilized (e.g., these CPUs may notbecome idle, and thus may not incur latency exiting deeper sleepstates). Furthermore, other CPUs not hosting ACS threads may enjoylonger idle periods and deeper sleep states, thus improving energyconsumption and, potentially, making more thermal headroom available forother unrelated threads, or allowing members of the ACS to run at higherclock rates.

Conversely, if concurrency restriction is not used, then the ACS may belarger. In this case, lock ownership can circulate over a larger numberof threads (CPUs) in a given period. Some of those threads may wait byblocking in the kernel, potentially making their CPUs become idle. Rapidcirculation of the lock over this larger ACS may cause CPUs to shiftbetween idle and non-idle more rapidly, both incurring latency in theidle to non-idle transition, and also prohibiting the CPUS underlyingthe ACS from reaching deeper energy-saving sleep states.

As noted above, the concurrency-restricting locks described herein maymaintain threads in an active circulation set and a passive set. Invarious embodiments in which the concurrency-restricting lock isNUMA-oblivious (e.g., not NUMA-aware), the initial placement of threadsin the active circulation set may be random or may be determined on afirst-come-first served basis. Note that a random initial partitioningof may be acceptable or even be desirable, and may allow the system to,eventually, sort itself out, especially in embodiments in which one ormore local or long-term fairness policies are imposed from time to time(e.g., policies that result in moving threads from the activecirculation set to the passive set and vice versa). As described in moredetail herein, these fairness policies (which may cause cullingoperations to move threads from the active circulation set to thepassive set or may promote threads from the passive set to the activecirculation set) may determine which threads are moved randomly (basedon a Bernoulli trial) or based on other criteria. For example, adecision about when to extract a thread from one of the sets and/orabout which thread to extract may, in different embodiments be based onwhich threads or nodes have or have not been active lately, on a countervalue or a length of time (e.g., how long threads have been waiting onthe passive set, or how many times threads in the active set have jumpedin front of those on the passive set).

One of the techniques employed by the concurrency-restricting locksdescribed herein may be a parking techniques (or a pair of operations topark( ) and unpark( ) a given thread.) In general a parking operationmay quiesce or passivate the calling thread and voluntarily surrenderthe CPU on which the caller was executing, making that CPU immediatelyavailable to run other ready threads. If no other threads are ready,then the CPU may become idle and be able to drop to lower power states.In some embodiments, this may reduce power consumption and may enableother threads on the same chip to run at faster speeds via turbo-mode.

Note that park( ) may, in some cases, admit spurious returns. One testof proper and safe park-unpark usage may be to consider the degeneratebut legal implementation where park( ) and unpark( ) are implemented asno-ops, in which case the algorithms that use park-unpark would simplydegenerate to spinning. This reflects a legal but poor qualityimplementation. After returning from a park( ) call, the caller isexpected to re-evaluate the conditions related to waiting. In thisexample, a park-unpark pair may be thought of as an optimized from ofbusy-waiting or polling. Specifically, control returning from park( )may not imply a corresponding previous unpark( ) operation. By allowingspurious wakeups, a system may afford more latitude to the park-unparkimplementation, possibly enabling useful performance optimizations. Notethat, given the point-to-point nature in which thread A directly unparksand wakes B, using park-unpark for a lock may require the lock algorithmto maintain an explicit list of waiting threads.

In some embodiments, optimized park-unpark implementations may be ableto avoid calling into the kernel. For example, if a thread S callsunpark(T) where T is not currently parked, the unpark(T) operation mayrecord the available “permit” in T's thread structure and returnimmediately without calling into the kernel. When T eventually callspark( ) it may clear that permit flag and return immediately, againwithout calling into the kernel. Redundant unpark(T) operations (inwhich a waiting thread T has previously been unparked but has not yetresumed) may also include an optimized fast path to avoid calling intothe kernel, in some embodiments. Note that park( ) may be augmented witha timed variation that returns either when the corresponding thread isunparked or when a predetermined time duration is exceeded.

In at least some embodiments of the systems described herein, theoperating system kernel scheduler may provide three states for threads:running, ready, and blocked. The running state indicates that the threadis active on a processor. The ready state indicates that the thread iseligible to run, but has not been dispatched onto a processor. Theblocked state indicates that the thread is suspended and ineligible fordispatch and execution. In some of the examples described herein, theterms “sleeping” or “waiting” may be equivalent to being in a “blocked”state. In at least some embodiments, park( ) may transition a runningthread to the blocked state and unpark( ) may transition a blockedthread to the ready state. The kernel may, typically, manage allready-running transitions, while the lock subsystem, via park-unpark,may controls the ready-blocked transitions. For example, the kernelscheduler's “dispatch” function may shift a thread from ready torunning. Involuntary preemption via time-slicing may shift a thread fromthe running state to the ready state. In general, park( ) may be thoughtof as causing a thread to “sleep” and unpark( ) may be thought of aswaking or resuming that thread, re-enabling the thread for subsequentdispatch onto a processor. In some embodiments, a parked thread may bewaiting for some event to occur, and notification of that event mayoccur via a corresponding consequent unpark( ).

In some embodiments, preemption may be controlled by the kernel and mayreflect an involuntary context switch. The victim is changed from therunning state to the ready state and some other ready thread may bedispatched on the CPU and made to run. In some embodiments, preemptionmay be trigged by timer interrupts. Typically, the kernel may resort topreemption when there are more runnable threads than CPUs. For example,the kernel may preempt one thread T running on CPU C in order to allowsome other ready thread a chance to run on C. Preemption may providelong-term fairness over the set of runnable threads competing for theCPUs. That is, the kernel may use preemption to multiplex M threads overN CPUs, where M>N. After a thread is made ready, it may receive a timeslice (quantum). When the quantum expires, the thread may be preemptedin favor of some ready thread. As previously noted, threads that havebeen preempted may be in the ready state. In some embodiments of thesystems described herein, concurrency restriction techniques may act toreduce preemption rates by reducing the number of ready threadscompeting for available CPUs. Recall that preemption is wasted“administrative” overhead and does not contribute to the forwardprogress of application threads. Furthermore, preemption mayinadvertently preempt the lock holder, resulting in a so-called “convoyphenomena”.

In some embodiments, optimized Park( ) implementations may spin brieflybefore reverting to blocking in the kernel. The spin period may be briefand bounded, and may act to reduce the rate of expensive and potentiallyunscalable calls into the kernel to perform ready-blocked statetransitions. This may be referred to as a spin-then-block waitingpolicy. As described herein, the spin period may reflect local spinningand may be implemented with a “polite” busy-wait loop or viaMONITOR-MWAIT instructions.

In some embodiments, waiting in the kernel via blocking or via aMONITOR-MWAIT mechanism targeting a thread-private local variable mayfree up pipeline resources or bring the CPU under thermal-energy caps,which in turn may accelerate the progress of the lock owner, increasingscalability. Note that if the lock is contended and fully saturated,throughput may be completely determined by the critical sectionduration. By potentially accelerating the lock owner, the criticalsection duration and lock hold time may be reduced.

In some embodiments, in order to help reduce handover latency, an“anticipatory warm-up” may be employed, as follows. If it is expect thata call to unpark( ) thread T will be made in the near future and if T isblocked in the kernel, thread T may be preemptively unparked so that Tbecomes ready and starts spinning. An Unpark(T) operation may imposeconsiderable latency in the caller because of the need to invoke kerneloperations. In some embodiments, an anticipatory unpark(T) may beexecuted only while the caller does not hold the lock for which T waits,otherwise the system may risk artificially increasing the criticalsection length and impacting throughput over the contented lock.Anticipatory unpark( ) operations may be particularly well suited forlocks that use succession by direct handoff, and may act to increase theodds that an unlock( ) operation will transfer control to a thread thatis spinning, instead of to a thread that is blocked in the kernel. Thisoptimization (which is optional) may help to reduce lock handoverlatency.

The concurrency-restricting lock described herein may, in differentembodiments, employ any of a variety of succession and waitingstrategies. For example, the concurrency-restricting lock algorithms mayprovide succession either by direct handoff (in which ownership of thelock is conveyed directly from the current owner to some waiting thread)or via so-called competitive succession (in which the current owner, inunlock( ) releases the lock and allows waiting threads to contend forthe lock) Direct handoff may perform better under high contention, whilecompetitive succession is more optimistic and may reduce successionlatency in conditions of light contention. To provide progress andliveness, locks that use competitive succession may need to unpark an“heir presumptive” thread that had been waiting. The heir presumptivemay then compete for the lock.

In some systems, and under certain conditions, locks that employ directhandoff succession may exhibit any or all of the following performanceissues: A) If the successor has been involuntarily preempted, then thelock might hand off ownership to a de-scheduled thread T. In this case,T may be either ready but not running, or may itself be blocked andwaiting on some other resource. This may result in the so-called“convoying” phenomena with transitive waiting (which is sometimesreferred to as “head of line blocking”). B) If the successor hasde-scheduled itself (e.g., via voluntary park( ) calls), it may need tobe awakened via unpark( ) This may be accomplished via calls into theoperating system kernel to make the thread eligible for dispatch. Thetime from an unpark( ) until the corresponding park( ) calls returns maybe very high because of overheads in the operating system. As previouslynoted, latencies of 10000 cycles or more may be typical. This lockhandover latency greatly impacts throughput over the contented lock, andcan dominate performance under contention.

In some embodiments, spin-then-park waiting strategies may provide somerelief from context switching costs. However, spin-then-park strategiesmay not work well with strict FIFO queue-based locks. With these typesof locks, the next thread to be granted the lock may also be the onethat has waited the longest, and is thus most likely to have exceededits spin duration and reverted to parking. Conversely, the most recentlyarrived threads may be the most likely to still be spinning, but theywill be the last to be granted the lock.

Note that all lock implementations that use local spinning also usedirect handoff, including, for example, list-based queuing locks inwhich threads contending for the lock are maintained in a linked listand spin on local variables, such as the locks described by JohnMellor-Crummy and Michael Scott (and which are sometimes referred toherein as “MCS locks”). In addition, all strict FIFO locks use directhandoff. As noted above, direct handoff is generally unsuitable forlocks that wait via parking. Park-unpark and waiting via local spinningtypically require the lock algorithm to maintain explicit lists ofwaiting threads. A simple TATAS lock (e.g., polite test-and-test-and-setlock) may employ competitive succession, and in comparison, may requireno such list be maintained, as the set of waiting threads is implicit.

In the discussions included herein, lock handover latency may bedescribed as follows. If thread A holds lock L, thread B waits for lockL, and thread B is the next thread to acquire ownership when thread Areleases L, the handover latency is the time between thread A's call tounlock( ) and the time at which thread B returns from lock( ) and canenter the critical section. Handover latency (which is sometimes called“responsiveness” in the literature) may reflect the overhead required toconvey ownership from A to B. Excessive handover latency increaseslatency and degrades scalability. For example, if A must unpark( ) B viacalls into the kernel to transition B from blocked to ready, then thehandover latency increases significantly. In some embodiments, theconcurrency-restricting lock implementations described herein mayattempt to minimize handover latency.

The manner in which a thread waits for lock may be referred to as thewaiting policy. Any of a variety of waiting policies may be employed inthe concurrency-restricting locks described herein including, indifferent embodiments, any of the policies described below. One examplewaiting policy is to use parking (as described above). Parking vacatesthe processor, allowing other threads to run. Unfortunately operatingsystem level context switching costs may be prohibitive when employingthis approach. Another example waiting policy is to use pure unboundedspinning. Note that both simple TATAS locks and classic MCS type locksuse unbounded spinning. While unbounded spinning often appears inacademic literature, it is generally avoided in actual software. Forexample, although it may be convenient and simple, unbounded spinningcan interfere with the performance of other threads on the system. Inaddition, spinning occupies a processor, possibly prohibiting some otherready thread from running. Eventually, involuntary preemption by theoperating system will de-schedule the spinner and allow other readythreads to run, but quanta (each time slice) can be relatively long.Therefore, depending on preemption may not be prudent and may result inparticularly poor performance when the number of ready thread exceedsthe number of available processors.

Another example waiting policy is a spin-then-park approach. Under thispolicy, threads may spin for a brief period (e.g., optimisticallywaiting) in anticipation of a corresponding unpark operation. Then, ifno unpark has occurred, they may revert to parking, as necessary. Underthis policy, the spin period (which constitutes local spinning) may beset to the length of a context-switch round trip. More precisely, athread may spins until I steps have passed or until a correspondingunpark occurs. In this example, I can be expressed in either units ofwall-clock time or in a number iterations of a spin loop. If no unparkoccurs within the period bounded by I, the thread may de-schedulesitself by parking.

In some embodiments, when using a spin-then-park waiting, policy, thespin phase may be augmented with a technique sometimes referred to as“inverted schedctl usage”. This technique may involve the use of aschedctl instruction (which allows a thread to request that involuntarypreemption be deferred for a brief period) to cover the spin phase of aspin-then wait mechanism, rather than to cover the critical section(which is the way it is typically used). In addition, the schedctlinterface may allow one thread to efficiently to query the kernelscheduling status (e.g., running, ready, or blocked) of any otherthread. For example, with MCS type locks, or more generally any lockwith succession by direct handoff, a thread U releasing the lock caninspect the schedctl status of the intended successor. For MCS typelocks, if the successor S is ready or blocked, then the successor is notspinning and transfer of ownership would entail considerable lockhandover latencies. In that case, the thread U calling unlock mayoptionally edit the MCS chain to excise the successor and favor handoffto some other thread T on the MCS chain that is spinning (running). Theexcised thread S may be placed on a special “standby” list. After Utransferred ownership of the lock to T, it may then try to wake S toprepare S to eventually receive ownership of the lock. Note that absentsuch a schedctl instruction, a protocol may be instituted in whichthreads in park( ) indicate (e.g., via status variables) if they arespinning or blocked in the kernel. Again, in order to reduce lockhandover latency, the succession policy may favor transferring ownershipto a thread that is spinning over a thread that is blocked in thekernel. In some embodiments, the schedctl approach may make it possibleto detect the case in which a thread that was spinning was preempted bythe kernel.

Another example of a waiting policy is one that uses MONITOR-MWAITinstructions. These instructions (or similar instruction pairs), whichare present on many modern processors, allow a thread to wait politelyfor a location to change. While waiting, the thread still occupies aCPU, but MWAIT allows the CPU to go into deeper sleep states. In someembodiments, hardware transactional memory may be used to wait politely.Normally, MWAIT may be inappropriate for global spinning with a largenumber of threads. However, the concurrency-restricting approachesdescribed herein may constrain the number of threads spinning on a givenlock at any moment, making MWAIT a viable option for the locks describedherein.

Note that, in general, a lock scheduling policy may be considered to be“work conserving” if the following invariant holds: if any waitingthread can be admitted, then one such thread will be admitted. Moreprecisely, a lock may be considered to be work conserving if it is neverthe case that: (a) the lock is unheld, and, (b) there are waitingthreads, and (c) none of those threads have been enabled for entry. Inpractice, for a lock that uses succession by direct handoff, if thereare waiting threads at the point when the lock is released, thenownership of the lock will be immediately conveyed to one of thosethread waiting threads. In the context of the concurrency-restrictinglocks described herein, the work conserving property means that theadmission policies may never under-provision the active circulation set(ACS). If the ACS needs to be re-provisioned or expanded by transferringan element from the passive set into ACS, then it is done immediately.This may avoid “dead time” during which the lock is not held, butwaiting threads could otherwise be allowed admission.

Note that in the example implementations described herein, a “freerange” threading model, in which the operating system is free to migratethreads between processors and nodes in order to balance load or achieveother scheduling goals, may be assumed. In addition, it is expected thatmigration (since it is relatively expensive) will be relatively rare.

Example Concurrency-Restricting Locks

The concurrency restriction techniques described above may be applied tocreate concurrency-restricting locks from a wide variety of existinglocks, in different embodiments. Several non-limiting examples ofconcurrency-restricting locks that are based on existing locks aredescribed in detail below. The first of these examples is an outer-innerdual path lock (sometimes referred to herein as an OIL lock) thatprovides concurrency restriction. In this example, theconcurrency-restricting lock includes an outer lock, an inner lock, anda field (referred to herein as the “IOwner” field) that indicates whichthread owns the inner lock. The concurrency-restricting OIL lock isconsidered to be held by a thread only if and only if that thread holdsthe outer lock. In this example, the outer lock may, in a sense, beconsidered the fast path for acquisition of the OIL lock, where thethreads in the active circulation set circulate and will move throughthe outer lock. On the other hand, the inner lock, where threads are putin the passive set, may be considered the slow path for acquisition ofthe OIL lock. The threads on the passive list wedge up against the innerlock, each hoping to acquire it and, in at least some cases, to begranted an opportunity to contend for the outer lock if they aresuccessful.

As described in more detail below, when a given thread first arrives atthe OIL lock, it may (optimistically) attempt to acquire the outer lock.If it is successful, it may return from its operation to acquire thelock as the OIL lock owner and may proceed to access the criticalsection of code or other shared resource that is protected by the OILlock. If the given thread is not able to obtain the outer lock, it mayspin or wait briefly (e.g., for 1000 cycles, or for some otherpredetermined number of cycles or amount of time) and may try again toacquire the outer lock. If, after a small number of attempts (with thesame or different amounts of spinning/waiting in between), the giventhread may give up and move on to the inner lock, joining the passiveset of threads that are contending for the inner lock.

In this example, once the given thread, or another thread in the passiveset, acquires the inner lock, it may take on a special status as theonly one of the threads in the passive set to be able to contend for theouter lock. For example, if there are ten threads contending for theinner lock, one of them (the inner lock owner) may be thought of asbeing poised between the active circulation set (threads of whichcontend for the outer lock) and the passive set (threads of whichcontend for the inner lock). After acquiring the inner lock, the innerlock owner may, from time to time, attempt to acquire the outer lock. Ifthe inner lock owner acquires the outer lock, it may return from itsoperation to acquire the lock as the OIL lock owner and may proceed toaccess the critical section of code or other shared resource that isprotected by the OIL lock. In some embodiments, when and if the innerlock holder (i.e., the inner lock owner) acquires the outer lock,another thread in the passive set may acquire the inner lock.

Note, however, that it may not be desirable for the inner lock holder toacquire the outer lock too quickly (or too often), as this may result infrequent intermixing between the active circulation set and the passiveset (thus defeating the purpose of partitioning incoming threads intotwo separate sets of threads in order to restrict concurrency).Therefore, in some embodiments, the inner lock holder may only attemptto acquire the outer lock occasionally and/or only after a predeterminedor randomized period of waiting or spinning following the acquisition ofthe inner lock or following a determination by the inner lock holderthat the outer lock is not held. For example, rather than the inner lockholder spinning on the outer lock in an attempt to quickly acquire theouter lock, it may perform what may be referred to as “anti-spinning”prior to attempting to acquire the outer lock. In this example, theinner lock holder may spin or wait for some period of time afteracquiring the inner lock (or after determining that the outer lock isnot held) before attempting to acquire the outer lock (e.g., in order toavoid acquiring the outer lock too quickly). If, after the inner lockholder spins or waits for a brief period, and in order to make progress,the inner lock holder determines that the outer lock is (still) notheld, the inner lock holder may attempt to acquire the outer lock(contending with the threads in the active circulation set for ownershipof the outer lock).

One embodiment of an outer-inner dual path lock (e.g., which is a typeof concurrency-restricting lock) is illustrated by the block diagrams inFIGS. 4A and 4B. In this example, the outer-inner dual path lock 400illustrated in FIG. 4A (which may, in different embodiments, protect acritical section of code in a multithreaded application or a sharedresource accessed by multiple threads of a multithreaded application)includes a data structure 460 that stores data representing the lockstate and/or other data associated with an outer lock (e.g., metadatarepresenting the lock owner or preferred/home node, a pointer to asuccessor for lock ownership, or other information), a data structure450 that stores data representing the lock state and/or other dataassociated with an inner lock (e.g., metadata representing the lockowner, shown as 455, metadata representing a preferred/home node, apointer to a successor for lock ownership, or other information), a datastructure 410 representing an active circulation set (e.g., a set ofthreads that circulate over the outer lock, contending for lockownership of the outer-inner dual path lock), and a data structure 440representing a passive set (e.g., a set of threads that are waiting foran opportunity to contend for ownership of the outer-inner dual pathlock).

In this example, outer-inner dual path lock 400 may be considered to beheld only when (and if) the outer lock is held. In other words, a threadholds the outer-inner dual path lock if and only if that thread holdsthe outer lock (regardless of whether or not it also holds the innerlock). In one example embodiment, described in more detail below, theouter lock may be a test-and-test-and-set lock and the inner lock may bea list-based queuing lock in which threads contending for the inner lockare maintained in a linked list and spin on local variables (e.g., anMCS type lock).

As illustrated in this example, a data structure representing the activecirculation set (such as data structure 410) may maintain datarepresenting multiple threads that (as indicated by the dashed linesfrom the elements 420 a-420 c to outer lock state/data 460) arecurrently contending for ownership of the outer lock in order to acquirethe outer-inner dual path lock (e.g., lock 400). As illustrated in thisexample, a data structure representing the passive set (such as datastructure 440) may maintain data representing multiple threads that, ingeneral, are not currently able to contend for ownership of theouter-inner dual path lock (e.g., at least elements 430 a-430 f), butthat contend for the inner lock. This is illustrated in FIG. 4A by thedashed lines between elements 430 a-430 f and inner lock state/data 450.

As illustrated by the bi-directional line between active circulation set410 and passive set 440, and described in detail herein, in variousembodiments, threads that are part of an active circulation set for aconcurrency-restricting lock may be culled from the active circulationset and placed in the passive set (thus limiting the number of threadsin active circulation) and/or threads that are part of the passive setfor a concurrency-restricting lock may be promoted to the activecirculation set (thus being afforded an opportunity to contend for theconcurrency-restricting lock and increasing long-term fairness withrespect to lock ownership). In the example outer-inner dual path lockillustrated in FIG. 4A, a thread that successfully acquires the innerlock (unlike any other threads in the passive set) may be able tocontend (along with the threads in the active circulation set) for theouter lock. In some embodiments, rather than explicitly joining theactive circulation set upon acquiring the inner lock, the thread thatholds the inner lock may, every once in a while (e.g., periodically orin response to various triggers), check the status of the outer lock. Insome embodiments, if the thread that holds the inner lock finds that theouter lock is not currently held, it may briefly spin or wait (e.g.,using an “anti-spinning” mechanism, several examples of which aredescribed herein) before attempting (one or more times, with or withoutadditional spinning or waiting in between) to acquire the outer lock.Once the thread that holds the inner lock acquires the outer lock, itmay join the active circulation set. For example, data representing thatthread may be moved from passive set 440 to active circulation set 410.

In this example, thread 430 a has acquired the inner lock, as indicatedby the pointer from the inner lock owner field 455 of inner lockstate/data 450. Therefore, thread 430 a may, once in a while, contendfor the outer lock, along with threads 420 a-420 e. This is illustratedin FIG. 4B, which depicts thread 420 a (with a dashed outline) withinactive circulation set 410 (as if it were actually a member of the setof threads in the active circulation set), and contending for the outerlock 460 (as indicated by the dashed line from the element representingthread 430 a in active circulation set 410 and outer lock 460). FIG. 4Balso depicts thread 420 a (with a solid outline) within passive set 440since, at this point, thread 420 a has not yet been promoted from beinga member of passive set 440 to being a member of active circulation set410. However, in some embodiments, when and if thread 430 a acquires theouter lock, it may access the critical section of code or sharedresource that is protected by the outer-inner dual path lock 400, afterwhich it may be promoted to active circulation set 410 and may releaseboth the inner lock (e.g., by modifying inner lock state/data 450) andthe outer lock (e.g., by modifying outer lock state/data 460).

In one embodiment of the outer-inner dual path lock described above, theinner lock may be an MCS lock, and the outer lock may be a simpletest-and-test-and-set (TATAS) spin lock. Test-and-test-and-set locks usecompetitive succession and offer excellent lock handover latency underno contention or light contention. With these locks, the set ofcontending threads is implicit and no list of waiting threads needs tobe maintained. However, under high contention, the performance of theselocks may suffer for any or all of the following reasons: 1) They cangenerate considerable futile coherence traffic while threads are pollingthe lock. This may be referred to as “global spinning”. Note that otherlocks (such as MCS locks) may instead use local spinning, such that atmost one thread is spinning on a given location at any instant. 2) If agroup of N threads are busy-waiting on a TATAS lock when the lock isreleased by the owner, all N threads may “pounce” simultaneously and tryto acquire the lock with an expensive atomic instruction that forces thecache line underlying the lock word into M-state, incurring yet morecoherence traffic. In this scenario, N−1 threads will fail and onethread will acquire the lock.

In some embodiments, e.g., in order to avoid the two issues describedabove, TATAS locks may employ randomized exponential back-off between agiven thread's attempts to acquire the lock. Back-off is anti-FIFO anddeeply unfair, however. In addition, a policy of applying such back-offsis also not strictly work conserving. The use of longer back-off periodscan reduce coherence traffic from polling, but such long periods mayalso increase hand-over latency. Somewhat perversely, the anti-FIFOproperty of TATAS locks can be useful for overall performance. Forexample, freshly arrived threads may probe more aggressively (e.g., at ahigher rate) and may thus be more likely to acquire the lock in anygiven period. Those freshly arrived threads may also have betterresidency in shared caches, while threads that have waited longer mayhave had their residency degraded and decayed by the actions of otherconcurrent threads. Note that the waiting threads have not been able tomake progress, accessing their data and maintaining their residency.

In some embodiments, the fairness of a TATAS lock may be tightly tied tothe underlying hardware cache arbitration polices of the system and/orits processor cores. In practice, TATAS locks are often deeply unfairbecause of various hardware properties. For example, threads “near” theprevious owner may enjoy some timing advantage in noticing that the lockhas been dropped (or is being dropped). However, this may beundesirable, as it may mean that a given lock may behave differentlyover a set of different hardware platforms. In many cases, the branchpredictors in TATAS loops may be trained in the wrong direction, causingbranch misprediction stalls at inopportune times, such as when the lockhas been observed to be “free” or just after the lock has been acquired.Despite those issues, a TATAS lock may be appropriate for the outer lockin an OIL construct. For example, this approach may reduce contention onthe outer lock to such a degree that the issues above are irrelevant,and the system may still benefit from the efficient lock handoverperformance conferred by TATAS locks.

In some embodiments, most of the contention for an OIL lock may bediverted to the inner lock on the slow path. In one embodiment, forexample, the inner lock may be a derivative of an MCS lock, which may beable to gracefully tolerate high contention. In some embodiments, allpotentially expensive operations on the inner lock may occur when athread does not hold the outer lock. This may avoid artificiallyincreasing the critical section duration (and thus, the lock hold time)for the purposes of performing administrative actions on the lock, suchas queue manipulation. In order to minimize the hold time, the amount oflock-related overheads that occur within the critical section may beminimized.

In some embodiments, the “OIL” transformation described herein may allowa lock such as an MCS lock, which uses direct handoff, to be convertedinto a composite form that allows barging. More specifically, the newconstruct may use direct handoff for threads in the slow contentionpath, but may allow competitive succession for threads circulatingoutside the slow path. The resultant composite “OIL” lock may thus enjoythe benefits of both direct handoff and competitive succession, whilemitigating the undesirable aspects of each of those policies.

One embodiment of a method for acquiring an outer-inner dual path lockis illustrated by the flow diagram in FIG. 5. As illustrated at 500, inthis example, the method may include a thread arriving at an outer-innerdual path lock that protects a critical section of code or sharedresource. The method may include the arriving thread attempting toacquire the outer lock (as in 510). If the thread successfully acquiresouter lock (shown as the positive exit from 515), the method may includethe thread accessing the critical section or shared resource that isprotected by the lock, and joining a set of threads that are circulatingover the outer lock (e.g., an active circulation set for the outer-innerdual path lock), as in 570. When the lock is no longer needed, themethod may include the thread that holds the lock releasing both theinner and outer locks, as in 575. Note that FIG. 7, which is describedin more detail below, illustrates one example of a method for performinga release (or “unlock”) operation and passing ownership of aconcurrency-restricting lock to another thread, according to at leastsome embodiments.

As illustrated in this example, if the thread fails to acquire the outerlock (shown as the negative exit from 515), the method may include thethread briefly spinning and/or waiting before making one or moreadditional attempts to acquire the outer lock. This is illustrated inFIG. 5 by element 520 and by the path from 520 to 525 and then from thenegative exit of 525 to 510. Once an applicable wait or spin limit hasbeen reached (shown as the positive exit from 525), the method mayinclude the thread joining a passive set of threads that are waiting(or, in other embodiments, are actively attempting) to acquire the innerlock (as in 530). As illustrated in FIG. 5, at some point, the methodmay include one of the threads in the passive set acquiring the innerlock, thus becoming the owner of the inner lock (as in 535).Subsequently, at some time later, the method may include the inner lockowner checking the status of the outer lock (e.g., to determine whetheror not it is currently held by another thread), as in 540. If the outerlock is currently held by another thread, the method may includerepeatedly (e.g., periodically or continuously) checking the status ofthe outer lock while waiting for its release. This is illustrated inFIG. 5 by the path from the positive exit of 545 to 540.

As illustrated in this example, if the outer is not currently held byanother thread (shown as the negative exit from 545), the method mayinclude the thread that owns the inner lock briefly waiting beforeattempting to acquire the outer lock (e.g., performing “anti-spinning”as described herein), as in 550. If, after the thread has waitedbriefly, the outer lock is still not held (shown as the negative exitfrom 555), the method may include the inner lock owner attempting toacquire the outer lock, as in 560. If the attempt is successful (shownas the positive exit from 565), the method may include the thread (theinner lock owner) accessing the critical section or shared resource thatis protected by the outer-inner dual path lock, and then joining the setof threads that are circulating over the outer lock (e.g., the activecirculation set for the outer-inner dual path lock), as in 570. As notedabove, when the lock is no longer needed, the method may include thethread that holds the lock releasing both the inner and outer locks, asin 575.

As illustrated in this example, if, after the thread has waited briefly,the outer lock is now held (shown as the positive exit from 555), themethod may again include the inner lock owner repeatedly (e.g.,periodically or continuously) checking the status of the outer lockwhile waiting for its release. This is illustrated in FIG. 5 by the pathfrom the positive exit of 555 to 540. For example, between the time thatthe outer lock was determined to be available (at 545) and the time whenits status was checked again (at 555), it may have been acquired by oneof the threads in the active circulation set and may no longer beavailable for acquisition by the inner lock holder. Note that, whilenote illustrated in FIG. 5, if and when another thread arrives at theOIL lock, some or all of the operations illustrated in FIG. 5 (e.g.,beginning with element 510) may be performed in parallel with thosedescribed above for the earlier-arriving thread. For example, a newlyarriving thread may contend with the earlier-arriving thread (at element510) if the earlier-arriving thread has not acquired the outer lock andhas not yet been placed in the passive set (e.g., while it is spinning,waiting, or re-attempting to acquire the outer lock, as in elements 520and 510). In another example, the newly-arriving thread may, afterfailing to acquire the outer lock, join the passive set of claims (as in530) and may contend with the earlier-arriving thread (and any otherthreads in the passive set) to acquire the inner lock, as in 530. Infact, in some cases, the newly-arriving thread, rather than theearlier-arriving thread or any other threads in the passive set, may bethe thread that acquires the inner lock (as in 535) and that goes on toattempt to acquire the outer thread (as in 540-565).

In at least some embodiments of the concurrency-restricting locksdescribed herein, a culling operation may be performed from time to timein order to reduce the number of threads in the active circulation setfor the concurrency-restricting lock (with or without regard to thenodes from which various threads in the active circulation set and/orthe passive set originated). For example, in some embodiments, a cullingoperation may be performed periodically by a dedicated process (e.g., byone of the threads of a multithreaded application that access theconcurrency-restricting lock or by a separate, dedicated thread), whilein other embodiments, a culling operation may be performed (or at leastinitiated) as part of an operation to release theconcurrency-restricting lock (e.g., by its current owner). As describedherein, the culling policy itself may be dependent on a lock saturationthreshold of the concurrency-restricting lock, on the value of aperformance metric associated with the concurrency-restricting lock, onwhich of the plurality of computing nodes each of the threads in theactive circulation set is executing on, and/or on other criteria, indifferent embodiments.

One embodiment of a method for culling an active circulation set of aconcurrency-restricting lock (e.g., an outer-inner dual path lock oranother type of concurrency-restricting lock, such as those describedherein) is illustrated by the flow diagram in FIG. 6. As illustrated at610, in this example, the method may include a thread beginning anoperation to cull one or more threads from the active circulation set ofthe concurrency-restricting lock. As described herein, such a cullingoperation may be performed at different times and/or may be triggered bydifferent events or conditions, in different embodiments. If theconcurrency-restricting lock is not a NUMA-aware lock (shown as thenegative exit from 620), the method may include moving one or morethreads from the active circulation set to the passive set of theconcurrency-restricting lock, as in 650. For example, moving threadsfrom the active circulation set to the passive set may involve movingdata representing those threads from one data structure or list (orportion thereof) to a different data structure or list (or portionthereof). More specifically (e.g., for an OIL lock), moving threads fromthe active circulation set to the passive set may involve moving datarepresenting those threads from a data structure that maintains datarepresenting a set of threads that are contending for the outer lock toa data structure that maintains data representing a set of threads thatare contending for the inner lock. As illustrated in this example, ifthe active circulation set still includes excess threads (according to aculling policy for the concurrency-restricting lock), the operationillustrated at 650 may be repeated one or more times until there are noadditional excess threads. This is illustrated in FIG. 6 by the pathfrom the positive exit of 660 to 650. If, or once, there are no excessthreads in the active circulation list (shown as the negative exit from660), the current culling operation may be complete (as in 670).

As illustrated in this example, if the lock is a NUMA-aware lock (shownas the positive exit of 620), and if the active circulation set includesexcess threads from one or more non-preferred (or remote) nodes (shownas the positive exit from 630, the method may include moving one or moreremote threads (threads executing on a non-preferred or remote node)from the active circulation set to the passive set, as in 640. As notedabove, moving threads from the active circulation set to the passive setmay involve moving data representing those threads from one datastructure or list (or portion thereof) to a different data structure orlist (or portion thereof), or may involve moving data representing thosethreads from a data structure that maintains data representing a set ofthreads that are contending for the outer lock of an OIL lock to a datastructure that maintains data representing a set of threads that arecontending for the inner lock of an OIL lock. If the active circulationset still includes excess threads from non-preferred (or remote) nodes,the operation illustrated at 630 may be repeated one or more times untilthere are no additional excess threads from non-preferred (or remote)nodes in the active circulation set. This is illustrated in FIG. 6 bythe path from the positive exit of 630 to 640 and from 640 to 630.

As illustrated in this example, even if (or once) all of the excessthreads that are executing on non-preferred (or remote) nodes have beenremoved from the active circulation set, this may or may not mean thatthere are no excess threads in the active circulation set, as therestill may be some excess threads in the active circulation set thatoriginated from the preferred (or home) node. For example, if, afterremoving all excess threads that originated from a non-preferred (orremote) node (shown as the negative exit from 630), the activecirculation set still includes excess threads (shown as the positiveexit from 660), the operation illustrated at 650 may be repeated one ormore times until there are no additional excess threads. In this case,the operation(s) may move excess threads that originated from thepreferred (or home) node. This is illustrated in FIG. 6 by the path fromthe negative exit of 630 to the input of 660, and from the positive exitof 660 to 650. If, or once, there are no excess threads in the activecirculation list (shown as the negative exit from 660), the currentculling operation may be complete (as in 670).

As noted above, in some embodiments of the concurrency-restricting locksdescribed herein, various culling operations may be performed (orinitiated), various fairness policies may be applied, and/or ownershipof the concurrency-restricting lock may be passed to another thread aspart of an operation by the current lock owner to release the lock. Oneembodiment of a method for releasing a concurrency-restricting lock(e.g., an outer-inner dual path lock or another type ofconcurrency-restricting lock such as those described herein) isillustrated by the flow diagram in FIG. 7. As illustrated at 710, inthis example, the method may include a thread beginning an operation torelease the concurrency-restricting lock. As described herein, thisoperation may be performed at different times and/or may be triggered bydifferent events or conditions, in different embodiments. For example,in some embodiments, the thread holds the lock owner may invoke anoperation to release the lock when it has finished executing a criticalsection of code that is protected by the lock or when it no longerrequires access to a shared resource that is protected by the lock. If(as shown by the positive exit from 720) there are no threads pending(e.g., if there are no threads currently contending for the lock as partof an active circulation set and there are no threads waiting for anopportunity to contend for the lock as part of a passive set), themethod may include releasing the lock (as in 725).

As illustrated this example, a release operation for aconcurrency-restricting lock may include (or trigger) a cullingoperation (e.g., one that may move one or more threads from an activecirculation set for the lock to a passive set for the lock) and/or theapplication of a long-term fairness policy (e.g., one that may move oneor more threads from a passive set for the lock to an active circulationset for the lock). For example, in some embodiments, a release operationmay include (or trigger) the releasing thread making a determination ofwhether or not a culling policy and/or a fairness policy should beapplied at that time and, if so, may include applying those policiesbefore releasing the concurrency-restricting lock and/or returning fromthe lock release operation. If (as shown by the positive exit from 730),a culling operation is indicated (e.g., according to an applicableculling policy), the method may include performing a culling operationon the active circulation set (as in 735). One example of such a cullingoperation is illustrated in FIG. 6 and descried above. However, if (asshown by the negative exit from 730) it is determined that a cullingoperation is not indicated at this time, the operation illustrated in735 may be elided. In either case, the method may include the releasingthread determining whether to pass ownership of theconcurrency-restricting lock to a thread in the active circulation setor to a thread currently in the passive set (as in 740).

If the releasing thread determines that ownership of theconcurrency-restricting lock is to be passed to a thread in the activecirculation set (shown as the positive exit from 750) and if theconcurrency-restricting lock is not a NUMA-aware lock (shown as thenegative exit from 760), the method may include the releasing threadpassing ownership of the concurrency-restricting lock to any thread inthe active circulation set (as in 765). In various embodiments, thethread to which ownership of the concurrency-restricting lock is passedmay be a thread that is at the head of (or is the next sequentialelement of) a queue, list, stack, or other data structure in which datarepresenting the threads in the active circulation set is maintained. Inother embodiments, the thread to which ownership of theconcurrency-restricting lock is passed may be selected from among thethreads in the active circulation set using some other mechanism and/orin accordance with a local fairness policy.

If the releasing thread determines that ownership of theconcurrency-restricting lock is to be passed to a thread in the activecirculation set (shown as the positive exit from 750) and if theconcurrency-restricting lock is a NUMA-aware lock (shown as the positiveexit from 760), the method may include the releasing thread passingownership of the concurrency-restricting lock to any local thread in theactive circulation set (as in 770). For example, the releasing threadmay pass ownership of the concurrency-restricting lock to a thread thatoriginated from the currently preferred (or home) node, which may or notbe at the head of (or the next sequential element of) a queue, list,stack, or other data structure in which data representing the threads inthe active circulation set is maintained, and which may be selected fromamong the local threads in the active circulation set using some othermechanism and/or in accordance with a local fairness policy, in someembodiments.

As illustrated in this example, if the releasing thread determines thatownership of the concurrency-restricting lock is not to be passed to athread in the active circulation set, but to a thread that is currentlyis the passive set (shown as the negative exit from 750) and if theconcurrency-restricting lock is not a NUMA-aware lock (shown as thenegative exit from 775), the method may include the releasing threadpassing ownership of the concurrency-restricting lock to any thread inthe passive set (as in 785). Here again, the thread to which ownershipof the concurrency-restricting lock is passed may be a thread that is atthe head of (or is the next sequential element of) a queue, list, stack,or other data structure in which data representing the threads in thepassive set is maintained, or may be selected from among the threads inthe passive set using some other mechanism and/or in accordance with alocal or long-term fairness policy, in different embodiments.

If the releasing thread determines that ownership of theconcurrency-restricting lock is not to be passed to a thread in theactive circulation set, but to a thread that is currently is the passiveset (shown as the positive exit from 750) and if theconcurrency-restricting lock is a NUMA-aware lock (shown as the positiveexit from 775), the method may include the releasing thread passingownership of the concurrency-restricting lock to a thread in the passiveset based on the node from which it originated (as in 780). For example,the releasing thread may pass ownership of the concurrency-restrictinglock to a thread that originated from a non-preferred (or remote) node,which may or not be at the head of (or the next sequential element of) aqueue, list, stack, or other data structure in which data representingthe threads in the passive set is maintained. In some embodiments, thethread to which ownership of the lock is passed may be selected fromamong the threads in the passive in accordance with a local or long-termfairness policy. For example, the successor thread may be selected in amanner that (over time) distributes ownership of the lock among threadsoriginating from different nodes or that, from time to time, changes thepreferred (or home) node, e.g., according to an applicable fairnesspolicy

In some embodiments, passing ownership of a concurrency-restricting lockto another thread may include releasing the lock after identifying thelock owner's successor. Note that, in some embodiments, passingownership of a concurrency-restricting lock to a thread in the passivelist may include promoting the thread to the active circulation set ofthe concurrency-restricting lock, while in other embodiments it mayinclude applying another mechanism to afford the thread the opportunityto contend for the lock (after which, if successful) it may be added tothe active circulation set. Note also that, in some embodiments (e.g.,when the concurrency-restricting lock is an outer-inner dual path lock),passing ownership of the concurrency-restricting lock to another thread(as illustrated at 765, 770, 780, and/or 785) may include releasing bothan inner and outer lock.

One embodiment of an outer-inner dual path lock that employs theconcurrency restriction techniques described herein may be illustratedusing the following example pseudo code:

01: class OIL:      // outer-inner dual path lock 02:  volatile intOuterLock   // optimistic path : test-and-test-set lock 03:  Thread *volatile IOwner  // Owner of InnerLock 04:  MCSLock InnerLock     //pessimistic path for contention 05: 06: Lock (Thread * Self, OIL * L) :07:  // Optimistic fast-path ... 08:  // ideally, threads in ACS willwait only briefly for the lock 09:  if TryLock (&L->OuterLock) : return10:  if TrySpin (&L->OuterLock) : return // brief bounded spin attempt11: 12:  // Revert to pessimistic and conservative slow path ... 13:  //Detected contention: excessive waiting ; cull and divert thread 14:  //must take pessimistic slow-path : move from active to passive 15: MCSAcquire (&L->InnerLock) 16:  assert L->IOwner == null  // at mostone “IOwner” thread at any     time 17:  L->IOwner = Self ; membarstore-load ; 18:  for 19:  if TryLock(&L->OuterLock) : break  // departpassive; join active 20:  Park( ) 21: 22: Unlock (Thread * Self, OIL *L) : 23:  assert L->OuterLock != 0 24:  L->OuterLock = 0   // releaseouter lock 25:  membar store-load 26:  auto s = L->IOwner 27:  if s ==null : return 28:  if s == Self : 29:  L->IOwner = null // acquired lockvia slow path 30:  MCSRelease (&L->InnerLock) 31:  else : 32:  //redundant unpark operations are expected to be cheap 33:  // optionaloptimization : skip the following unpark( ) 34:  // if the existence of“visible” spinners can be detected at line 10 35:  Unpark (s)   // wakethread parked at line 20

As previously noted, in some embodiments (including the embodimentillustrated by the pseudo-code above) an OIL lock is considered to beheld if and only if the outer lock is held. There is no long-termwaiting on the outer lock, however. In the example OIL embodimentillustrated above, the outer lock is implemented as a politetest-and-test-and-set TATAS lock and the inner lock is implemented as anMCS lock. However, other combinations of lock types are possible andreasonable, in other embodiments. In this example, the MCS lock thatimplements the inner lock has been augmented to use a simplespin-then-park waiting strategy, rather than using spinning. In thisexample, when a thread first arrives at the OIL lock, it may(optimistically) attempt to acquire the outer lock. If it is successful,it may return from its operation to acquire the lock as the lock owner.This is shown in the pseudo-code above by the return on line 9.

In this example, threads circulating over the outer lock or spinning atline 10 form the active circulation set (ACS), while threads blocked onthe inner lock at line 15 constitute the passive set (PS). Here, the ACSis implicit and the passive set is explicit in the MCS queue. Moreprecisely, the ACS consists of the current owner, threads spinning atline 10, and circulating threads that are currently executing in theirrespective non-critical sections. In this example, once one of thethreads in the passive set acquires inner lock (at line 15), it moves onto line 16, where it installs itself in as the new owner (holder) of theinner lock. As described herein, the inner lock holder may, from time totime, attempt to inject itself into the active circulation set andcontend for the outer lock, while the other threads in the passive setremain blocked (e.g., waiting for a chance to acquire the inner lockand, consequently, an opportunity to contend for the outer lock).Restricting the threads that can contend for the outer lock to those inthe ACS plus (occasionally) one of the threads from the passive set, andlimiting the opportunities for the inner lock holder to acquire thelock, may limit the number of times at which, if contention on the outerlock drops, a thread from the passive set is able to acquire the outerlock (thus intermixing the ACS and the PS more often than may bedesirable).

In the example implementation illustrated above, the outer lock usescompetitive succession to identify the thread that will become the nextowner, and the inner lock (the MCS lock) uses succession by directhandoff. In this example, the single “IOwner” thread waiting at lines18-20 may be thought of as being in transitional state between thepassive and active sets. The IOwner thread may monitor the outer lock,attempting to acquire it only if (after giving the threads in the ACS achance to acquire it following its release) no other thread takes it. Toprovide long-term fairness, if a thread has languished too long in theloop at lines 18-20, an anti-starvation technique may be applied.Specifically, if the IOwner has waited too long, it can become“impatient”. One approach may be to have that thread spin moreaggressively. Another technique may be to arrange for direct successionto that thread the next time unlock( ) is called. This may yield ahybrid succession that uses competitive succession as the default, butthat conservatively reverts to direct succession to ensure progress andfairness.

In the example illustrated above, the park(T) interface may allow athread T to “park” or block itself, rendering the caller ineligible tobe scheduled or dispatched. A corresponding unpark(T) may wake thetarget thread T, making it again ready for dispatch. Note that, in someembodiments, an unpark(T) operation can occur before the correspondingpark( ) by T, in which case park( ) returns immediately and “consumes”the pending unpark( ) action. In some embodiments, the park-unparkfacility may be implemented via a restricted-range semaphore, e.g., onethat only allows values of 0 (representing a neutral state) and 1(representing a state in which an unpark operation is pending).

In some embodiments (e.g., on platforms in which memory fences arerelatively costly), the memory barrier shown at line 25 above may, insome embodiments, be elided if the park( ) call at line 20 is modifiedso that it uses a timed wait, instead. Eliding the barrier may admit arace on platforms with weak memory consistency models where the programorder of “ST OuterLock; LD IOwner” may be architecturally reordered to“LD IOwner; ST OuterLock,” in memory-visibility order. In turn, thisorder may allow a race where a thread in unlock( ) could fail to noticea newly-arrived thread and could unpark( ) the newly-arrived thread inthe lock( ) path. The timed park( ) operation may recover from thisscenario.

As noted above, to reduce the rate of the flow of threads from thepassive set to the active circulation set, the somewhatcounter-intuitive concept of “anti-spinning” may be applied. Forexample, threads may typically spin while trying to acquire lock.However, an anti-spinning mechanism may attempt to avoid taking a lockover a short bounded period. For example, for various reasons,succession policies may prefer that one of the threads already in theACS acquire the outer lock next (when it is released by the currentowner of the outer lock), rather than the IOwner thread waiting at lines18-20. Ideally, a thread that has just arrived at the OIL lock (a threadthat has just invoked a lock( ) operation) will be able to acquire theouter lock. However, if the IOwner thread acquires the lock at thatpoint, assuming a steady-state load and just enough active threads tosaturate the lock, some other thread in the ACS will fail its spinningattempt at line 10 and move into the passive set. This migration mayincrease the LWS size, which may be undesirable. In other words, theIOwner thread may (by acquiring the outer lock) displace some othermember of the ACS, while it may be preferable for the ACS and passivesets to remain relatively stable. In some embodiments, an anti-spinningtechnique may be applied in order to reduce the rate of suchdisplacement and to maintain more stable partitions between the ACS andthe PS.

Two specific implementations of anti-spinning are illustrated in thecode above. In the first example, the TryLock operator (found at line19) does the anti-spinning. Here, the IOwner thread monitors the outerlock. Normally, if a thread is spinning waiting for a lock and observesthat the lock has just been released, it immediately tries toaggressively “pounce” on the lock to acquire it. However, withanti-spinning, if the IOwner sees that the outer lock is not held, itmay defer for some period (giving newly-arriving threads or threadsalready in the ACS a chance to obtain the outer lock) before attemptingto acquire the outer lock. In this example, it is hoped that a member ofthe ACS will acquire the lock in the interim. In other words, theanti-spinning may prevent the IOwner thread from being too aggressive inobtaining the outer lock since, if the lock circulates too rapidlybetween the ACS and PS, it may not perform any better than if noconcurrency restriction techniques were applied at all. Eventually, toensure progress and liveness, the IOwner may need to claim the lock,e.g., if there are no members of the ACS present to do so.

Quite a number of anti-spinning policy variations are possible, indifferent embodiments. One example policy that has worked in practice inprototypes is for the IOwner thread to check the outer lock N times in arow with fixed delays between the checks. If the number of times itobserved that the outer lock was free during the sampling episodeexceeded some bound B, then it will try to acquire the lock. Thistechnique may provide a relatively simple way to detectunder-saturation. An example variation on the policy described above(one that may improve long-term fairness) may be to gradually decrease Nand B between sampling episodes. Note that it may be preferable to makethe IOwner thread less aggressive, and to defer (within reason) toarriving threads in the ACS.

Another example anti-spinning technique is illustrated at line 35 in thepseudo-code shown above. Here, the unpark( ) operation attempts to wakethe IOwner thread. However, if, while spinning briefly just before line35, some other member of the ACS takes the lock during that spinningperiod, there may be no need to wake the IOwner thread. In this example,since the IOwner does not wake up, it will not contend for the lock, andthe ACS and passive sets will remain intact. Note that a non-redundantunpark( ) operation may require a system call into the kernel to wakethe target thread. Avoiding such expensive and high-latency calls mayimprove performance, in at least some embodiments.

As previously noted, the example outer-inner dual-path lock illustratedabove implements a hybrid waiting policy in which recently arrivedthreads wait (via brief and bounded global spinning) on the outer lock.If they fail to acquire the outer lock, they revert to the inner lock,where, as part of the spin-then-park protocol in MCSAcquire( ) they spinlocally. Finally, failing that, the acquisition attempt reverts toparking. Note that, in this and other examples, it is expected that therate of circulation within the ACS will be much faster than circulationbetween the active circulation set and the passive set. Note also that,in some embodiments, spin-then-park waiting mechanisms may beimplemented under the park-unpark interface, and are not shown in thepseudo-code above.

As shown in the example above, if the IOwner acquires the outer lock andexecutes the critical section protected by the OIL lock, it may have anadditional duty to perform when it no longer needs the lock. In thiscase, it may release the outer lock, as usual, and then may also have torelease the inner lock. In other words, the release of the inner lockmay be deferred until the IOwner is finished executing the criticalsection, at which point it may release both the inner and outer locks.This is illustrated at lines 29-30 above. Holding the inner lock whileexecuting the critical section may prevent other threads in the passiveset from being granted an opportunity to contend for the outer lock.

In some embodiments (e.g., in order to further restrict and constrainconcurrency), the implementation of TrySpin( ) shown in Line 10 above,may be configured to restrict or cap the number of threads spinning onthe outer lock at any given moment. In some embodiments, TrySpin( ) mayapply a test-and-test-and-set policy with back-off. A complementary“local” policy in the TrySpin( ) implementation may abandon the currentspin episode if the “test-and-set” atomic operation fails toofrequently. This condition may indicate a sufficient flow of threads inthe ACS over the lock. Some embodiments may maintain a count of thenumber of such failures and may abandon the spin attempt when and if thecount exceeds some predetermined bound. In another embodiment, after atest-and-set failure, a Bernoulli trial may be performed (e.g., via asimple uniform pseudo-random number generator with thread-local state)in order to decide whether to abandon the spin attempt. In yet anotherimplementation, TrySpin( ) may be configured monitor either traffic overthe lock or the arrival of spinners, and to abandon the spin attempt ifthe rate or flux is too high (e.g., if it exceeds a predeterminedthreshold value). In this example, by abandoning the spin attempt early,the thread may revert from spinning to parking.

Note that, in this and other examples, all atomic operations may beassumed to have full bi-directional fence/member semantics. Note alsothat, in the example pseudo-code listings included herein, a total storeorder (TSO) memory consistency model may also be assumed.

While the example outer-inner dual path lock described above employs atest-and-test-and-set lock as the outer lock and an MCS type lock as theinner lock, other types and/or combinations of locks of can be employedas the inner lock and outer locks, in other embodiments. Note that, ifthe inner lock is itself NUMA-friendly (e.g., if it is a “cohort” lock)then the aggregate “OIL” lock may also be NUMA-friendly. For example, asthreads circulate between the active circulation set and the passiveset, the inner lock may tend to cull or filter out threads fromdifferent nodes, and the active circulation set may tend to convergetoward a set of threads that are co-located (co-resident) on a givennode. In this case, the NUMA-diversity of the ACS may decrease and thenumber of lock migrations may also decrease, yielding better throughput.

In one example alternative OIL-type locking scheme (referred to hereinas an OILA lock), the inner MCS lock may be replaced with an arrivallist and outflow list. In this example, newly arrived threads enqueueonto the arrival list in a lock-free fashion. Here, threads passingthrough the slow contention path undergo the following statetransitions: Arrival List→OutFlow List→IOwner→Ownership. In thisexample, which is illustrated in more detail by the example pseudo-codebelow, the arrival list may be able to safely tolerate multipleconcurrent “push” operations, but only one “pop” or detach operation ata time. In some embodiments, this lock may employ an MPSC (multipleproducer single consumer) access model with multiple concurrentproducers but at most one consumer, and may be immune to ABA corruption.Collectively, ArrivalList, OutFlow and IOwner constitute a singlelogical list of threads that are stalled waiting for the OILA lock.Using two distinct lists may reduce coherence contention on the listends.

In this OIL variant, the “IOwner” variable may act as a lock to ensurethat there is at most one active pop or detach operation at any giventime. In other words, the IOwner may serve as an “inner lock” thatprotects the arrival and outflow lists. In this example, only the threadis the IOwner can operate on the outflow list or detach the arrival listand drain the contents into the outflow list. IOwner may be consideredasymmetric in that one thread can acquire that lock and pass ownershipof IOwner to a successor, and the successor will ultimately release orpass ownership of IOwner. Note that, in this example, there may never becontention or waiting for the inner IOwner lock. Instead, only “trylock”operations are required, and ownership of IOwner is passed via directsuccession. Note also that the IOwner thread is conceptually at thefront of the logical list (the combined single logical list of threadsthat are stalled waiting for the OILA, and that includes thelockArrivalList, OutFlow list and IOwner).

As noted above, in this example, at any given time there can be at mostone IOwner thread for a given OILA lock. The IOwner thread is one thatis waiting for the OILA lock but has been unlinked from the arrival andoutflow lists. Once a thread has been designated as the IOwner thread,it may remain so until it manages to acquire the outer lock. As in theOIL lock described earlier, the IOwner thread may be eligible to try toacquire the outer lock, while threads resident on the arrival list oroutflow list may not have that opportunity, but must wait for their turnto become the IOwner thread.

As illustrated in the pseudo-code for the OILA shown below, at unlock( )time, if a tentative successor thread needs to be selected, the firstplace to look for a successor may be the OutFlow list. If a thread isselected from the OutFlow list, this thread becomes the next “IOwner”thread. If the OutFlow list is empty, an attempt may be made to transferthe Arrival list into the OutFlow list.

Note that, in this and other examples, the “CAS” primitive may representan atomic compare-and-swap type operation in which the first argument isthe memory location, the second argument is the comparand, and the finalargument is the value to which the memory location should be set if thememory location equals the comparand. A CAS-type operation returns thevalue that was previous stored in the target memory location. Similarlythe “SWAP” primitive may represent an operation that atomically fetchesa value from a target location and stores a new value into that samelocation, returning the fetched value. The SWAP operator atomicallyloads the value from the address given as the first argument, stores thesecond argument into that address, and then returns the original value.As noted above, in the example pseudo-code listings included herein, allatomic operations may be assumed to have full bi-directionalfence/member semantics, and a total store order (TSO) memory consistencymodel may also be assumed.

One embodiment of the OILA lock mechanism described herein may befurther illustrated by the example pseudo-code below.

01: class OILA :       // alternate outer-inner dual path lock 02: volatile int OuterLock    // optimistic path : test-and-test-set lock03:  Thread * volatile IOwner 04:  Thread * volatile Arrival 05: Thread * volatile OutFlow 06: 07: Succession (Thread * Self, OILA * L): 08:  for 09:  // this thread owns IOwner lock 10:  assert L->IOwner ==1 11:  // first, try to draw a thread from the Outflow list 12:  autoList = L->OutFlow 13:  if List != null : 14:   PickFromList : 15:   //pop front of List as successor 16:   L->OutFlow = List->Next 17:   //appoint thread referenced by “List” as new IOwner thread 18:   // Passownership of IOwner to successor 19:   L->IOwner = List 20:   Unpark(List) 21:   return 22:  assert L->OutFlow == null 23:  // resort toArrival List : RATs = recently arrived threads 24:  // bulk en-masstransfer from the Arrival list into Outflow 25:  // detach and privatizelist of RATs; drain from Arrival to Outflow 26:  List = SWAP(&L->Arrival, null) 27:  if List != null : 28:   // optionally reverselist or sort by NUMA node ID 29:   // impose desired queue disciplineand ordering 30:   // Reversing the list in-hand yields pure strict LIFO31:   // Using the list in ambient order yields an admission order 32:  // akin to “palindrome” or “elevator seek” schedules 33:   gotoPickFromList 34:  // no apparent successor threads; release IOwner innerlock 35:  L->IOwner = 0 36:  membar store-load 37:  // ratify andre-validate apparent lack of successor threads 38:  // note that itneeds to check only that L->Arrival, and not     L->OutFlow 39:  ifL->Arrival == null : return 40:  // if some other thread holds theIOwner lock, then responsibility 41:  // for succession passes to thatthread through delegation 42:  if CAS (&L->IOwner, null, 1) != null :return 43: 44: Lock (Thread * Self, OILA * L) : 45:  if TryLock(&L->OuterLock) : return 46:  if TrySpin (&L->OuterLock) : return 47: // must take pessimistic slow-path 48:  // Optional optimization : tryto “barge” directly on IOwner 49:  assert L->IOwner != Self 50:  if CAS(&L->IOwner, null, Self) == null : goto StagingLoop 51:  // Enqueue Selfon Arrival List : CAS-based “push” 52:  auto h = L->Arrival 53:  for 54: Self->Next = h 55:  auto v = CAS (&L->Arrival, h, Self) 56:  if v == h: break 57:  h = v 58:  // detect and recover from a potential race withunlock( ) 59:  // where owner drops Outer lock and departs and then 60: // this thread enqueues itself onto Arrival list 61:  // absentrecovery, this thread could be stuck indefinitely on Arrival 62:  //list and fail to advance to IOwner position 63:  // precautionarysuccession - avoid progress and liveness failure 64:  // only the threadthat transitioned L->Arrival from null to non-null 65:  // must performthis check 66:  if h == null && CAS (&L->IOwner, null, 1) == null : 67: Succession (Self, L) 68:  // wait while thread resides on Arrival orOutflow lists 69:  while L->IOwner != Self : 70:  Park( ) 71:  // Selfhas moved from ArrivalList/OutFlow to IOwner 72:  // can now compete forlock 73:  StagingLoop : 74:  for 75:  assert L->IOwner == Self 76:  ifTryLock(&L->OuterLock) : break 77:  Park( ) 78: 79: Unlock (Thread *Self, OILA * L) : 80:  assert L->OuterLock != 0 81:  L->OuterLock = 082:  membar store-load 83:  auto s = L->IOwner 84:  if s == null || s ==1 : return 85:  if s == Self : 86:  // this thread acquired the lock viathe slow path 87:  // optional optimization to reduce rate ofunnecessary unpark     operations 88:  // Mark IOwner as busy 89: L->IOwner = 1  // overwrite Self with 1 90:  Succession (Self, L) 91: else : 92:  Unpark (s)

In some embodiments, a concurrency-restricting lock may be constructedfrom a lock that employs last-in-first-out (LIFO) admission ordering. Insome embodiments, a LIFO lock may be deeply unfair. However, variants ofLIFO locks may be configured to provide concurrency restriction,NUMA-awareness, and/or long-term fairness, in different embodiments.Before describing one such variant, a more typical LIFO lock ispresented below. In this example, in order for a thread to acquire theLIFO lock, it may use an atomic CAS-type operation to attempt to updatethe state of the LIFO lock. The lock state may be either 0 or 1, or maypoint to a stack of threads that are waiting to acquire the LIFO lock.If, when a thread arrives at the LIFO lock, its state is 0, it may use aCAS-type operation to attempt to change the state from 0 to 1. If thisis successful, the newly-arrived thread may acquire the lock and proceedto access the critical section of code or shared resource that isprotected by the LIFO lock. However, if, when a thread arrives at theLIFO lock, its state is not 0 (e.g., if its state is 1 or a value otherthan 0 or 1), this may indicate that the lock is held by another threadand/or that there is a stack of threads waiting to acquire the LIFOlock. In this case, the newly-arrived thread may use an atomic CAS-typeoperation to push itself onto the front of the stack of threads that arewaiting to acquire the LIFO lock. Again note that, in this example,because the threads are waiting on a stack, the admission order followsLIFO ordering and is deeply unfair. One embodiment of a LIFO lock isillustrated by the example pseudo-code below.

01: class LIFOLock : 02:  Thread * volatile Head   // waiting threadsform a stack 03: 04: Lock (Thread * Self, LIFOLock * L) : 05:  auto w =L->Head 06:  top: 07  if w == 0 : 08:  // uncontended lock acquisitionpath 09:  w = CAS (&L->Head, 0, 1) 10:  if w == 0 : return 11:  // CASfailed : this thread raced another and lost 12:  // inopportuneinterleaving - concurrent interference; L->Head     changed 13:  //Contended - thread must wait - thread pushes Self onto stack 14: Self->Grant = 0 15:  Self->Next = w 16:  auto v = CAS (&L->Head, w,Self) 17:  if v != w : 18:  w = v 19:  goto top  // CAS failed; retry20:  // successful push; begin waiting phase 21:  while Self->Grant == 0: 22:  Park( ) 23:  assert L->Head != 0 24: 25: Unlock (Thread * Self,LIFOLock * L) : 26:  auto w = L->Head 27:  assert w != 0 28:  if w == 1: 29:  w = CAS (&L->Head, 1, 0) 30:  if w == 1 : return 31:  assert w !=0 && w != 1 32:  // there is at least one thread on the stack 33:  //while the lock is held, the stack only grows 34:  auto nxt = w->Next 35: assert nxt != null && nxt != Self && nxt != w 36:  // try to pop thehead from the stack 37:  auto v = CAS (&L->Head, w, nxt) 38:  if v == w: 39:  // successful pop 40:  assert w->Grant == 0 41:  w->Grant = 1 42: unpark(w) 43:  return 44:  // The CAS failed 45:  // The only source ofinterference is newly arrived threads that push 46:  // themselves ontothe stack. 47:  // Thus there must be at least two threads on the stack.48:  // At this point the implementation has the option of retrying, 49: // or just extracting and waking the 2nd thread. 50:  // This yields aplausibly LIFO history, reduces coherence traffic 51:  // on the Stackfield, and yields an unlock( ) that runs in constant-time 52:  // withno loops. 53:  assert v != 0 && v != 1 54:  nxt = v->Next 55:  assertnxt != 0 && nxt != 1 56:  // splice nxt out of the stack 57:  v->Next =nxt->Next 58:  assert nxt->Grant == 0 59:  nxt->Grant = 1 50: unpark(nxt)

In the example LIFO lock implementation illustrated above, the waitingthreads form a stack. Thus, the lock (which provides succession bydirect handoff) employs LIFO admission ordering. Note that LIFO orderingnaturally provides concurrency restriction, albeit deeply unfair.Conceptually, the passive set, in this example, consists of threads thatreside toward the tail (suffix) of the stack. In this example, arrivingand departing threads access the same “Stack” field, creating acoherence hot-spot. In this example, the lock is held if and only if theHead value is non-zero. The scenario in which the Head value is 1 may bea special encoding that indicates the lock is held but that no threadsare waiting, while a Head value of 0 indicates that the lock is notheld. If the Head value has other value, T, this may indicate that thelock is held and that the thread pointed to by T is the head (top) ofthe linked list forming the stack of waiting threads. In this example,the stack is intrusively linked through the thread “Next” fields, andthe final thread in the stack has a Next value of 1.

In the example illustrated above, the stack has a multiple producersingle consumer (MPSC) access model. Here, any arriving thread (e.g.,any thread that encounters contention upon arrival at the LIFO lock andmust wait to acquire it) can push itself onto the stack at any timeusing a lock-free CAS-type operation. However, only the current LIFOlock owner can perform a pop operation. Thus, the mutual exclusionproperties of the lock itself may ensure that there is at most oneconsumer (pop operation) at any given time. In at least someembodiments, this may be sufficient to avoid any ABA stack corruptionpathologies that can manifest under general MPMC access.

As noted above, a LIFO lock may provide succession by direct handoff.Typically, a direct handoff succession policy works well with aspin-then-park waiting strategy. However, the LIFO lock may be anexception. In this example, the most recently arrived waiting threadsreside near the head of the stack, and are most likely to be spinning.If the unlock( ) operator pops an element from the stack, that thread ismore likely to be spinning than any of the other threads on stack. Onthe other hand, threads toward the end of the stack are more liable tobe parked.

As described above, LIFO admission ordering may be inherently unfairover the short term. However, in some embodiments, some long-termfairness may be imposed by occasionally passing ownership of the LIFOlock to the thread at the tail of the stack rather than to the thread atthe head of the stack. A wide variety of possible implementations arepossible. In some example embodiments, ownership may be passed to thetail of the stack based on (a) Bernoulli trials via a thread-localuniform pseudo-random number generator (b) a counter that is incrementedon each acquisition until it reaches some predetermined upper bound or(c) the amount of time that a thread has waited. In one specificexample, a counter may be used to measure how many times the tail hasbeen bypassed in favor of more recently arrived threads, yielding abounded bypass policy.

In some embodiments, through various transformations, a LIFO lock suchas that described above may be made NUMA-friendly. For example, aNUMA-friendly (or NUMA-aware) variant of the LIFO lock may beconstructed such that the unlock( ) operator occasionally checks to seeif the thread at the head of the stack (e.g., a thread that is about tobe popped from the stack) is executing on the currently preferred (orhome) NUMA node in a NUMA system. If not, unlock( ) may be configured topop that element and either move that remote thread to the tail of thestack, or (as with the MCSCR lock described below), move that remotethread to an explicit “cold” list.

One embodiment of NUMA-aware last-in-first-out type lock (e.g., a LIFONlock, which is one example of a concurrency-restricting lock) isillustrated by the block diagram in FIG. 8. In this example, the LIFONlock 800 illustrated in FIG. 8 (which may, in different embodiments,protect a critical section of code in a multithreaded application or ashared resource accessed by multiple threads of a multithreadedapplication) includes a data structure 860 that stores data representingthe lock state and/or other data associated with an outer lock,including a pointer to the top of the main LIFO stack 810, whichindicates what may (in many cases) be the successor to the current lockowner. The state/data information may also include metadata representingthe current lock owner, a preferred/home node, and/or other informationabout the lock and its state. In this example, LIFON lock 800 includes amain stack 810 that stores data representing an active circulation set(e.g., a set of threads that circulate over the outer lock, contendingfor lock ownership of the LIFON lock), and data representing a passiveset (e.g., a set of threads that are waiting for an opportunity tocontend for ownership of the LIFON lock). Here, the active circulationset, shown as ACS 815 in FIG. 8, may include the threads that arenearest to the head of main stack 810 (e.g., up to a pre-determinednumber of threads) and the passive set, shown as excess (PS) 825 in FIG.8, may include threads that are nearer to the tail of main stack 810(the remaining threads in main stack 810). In this example, threads 820c-820 f may be considered to be members of active circulation set 815,while threads 820 a and 820 b may be considered to be members of passiveset 825. In some embodiments, all of the threads in the passive set 825may be excess local threads (e.g., they may be threads that originatedfrom the currently preferred/home node). As illustrated by the downwardpointing arrows in main stack 810, the succession of the lock ownershipmay, in at least most cases, be handled by direct handoff to the nextthread in main stack 810.

In this example, LIFON lock 800 also includes a remote list 840, e.g., adata structure that stores information about additional excess threads,including threads that originate from one or more computing nodes otherthan a current preferred/home node. In some embodiments, if the threadthat is at the head of main stack 810 originated from a node other thanthe preferred/home node, it may be moved to remote list 840, rather thanbeing allowed to assume ownership of the LIFON lock when the currentowner releases the lock. Here, remote list 840 includes, at least,threads 850 a-850 j. As illustrated by the bi-directional line betweenmain stack 810 and remote list 840, and described in more detail herein,in various embodiments, threads that are part of the active circulationset (or even the passive set) for a LIFON lock may be culled from themain stack 810 and placed on the remote list 840 and/or threads that arepart of the remote list 840 for a LIFON lock may be promoted to the mainstack 810, e.g., by being pushed onto the head of main stack 810,becoming part of active circulation set 815 (thus being afforded anopportunity to contend for the LIFON lock and increasing long-termfairness with respect to lock ownership). For example, the applicationof a long-term fairness policy may, from time to time, cause a thread atthe head of remote list 840 to be promoted to main stack 810.

As illustrated by the downward pointing arrows in remote list 840, theselection of a thread to be promoted from remote list 840 to main stack810 may, in at least most cases, be performed according to LIFOordering. However, in other embodiments, the selection of a thread to bepromoted from remote list 840 to main stack 810 may be based on anothertype of ordering, on the computing nodes from each the threads on remotelist 840 originate, or on other criteria (e.g., according to anapplicable long-term fairness policy). In some embodiments, theapplication of a local or short-term fairness policy may, from time totime, cause a thread at the tail of main stack 810 to be pulled from thetail position and pushed onto the head of main stack 810.

Note that FIG. 8 does not necessarily illustrate all of the details orcomponents of a LIFON lock structure, but includes details that arerelevant to the implementation of the concurrency-restricting techniquesdescribed herein. Note also that a NUMA-oblivious LIFO lock (such asthat described earlier) may be similar to the LIFON lock illustrated inFIG. 8, but would not include the remote list 840.

As noted above, a LIFON lock may be a NUMA-friendly variant of a LIFOlock, and may also provide concurrency restriction. As was the case withthe NUMA-oblivious LIFO lock, the main list of waiting threads forms astack, and the LIFON lock provides succession by direct handoff. In thisexample, surplus local threads reside at the suffix of the stack. Thismay obviate the need for an explicit list of passive local threads. Incontrast, surplus remote threads may reside on an explicit “Remote”list. In at least some embodiments, only the current owner may be ableto access the Remote list. As shown in the example pseudo-code below,all editing of the main stack may occur at unlock time within theLockRelease( ) operation, which may include a variety of differenttriggers, several of which are described in reference to the exampleLIFON pseudo-code shown below.

One embodiment of a method for releasing a NUMA-aware LIFO lock (e.g., aLIFON lock) is illustrated by the flow diagram in FIG. 9. As illustratedat 910, in this example, the method may include a thread beginning anoperation to release a LIFON lock. If an applicable fairness mechanism(e.g., according to a long-term fairness policy) favors a remote thread(e.g., a thread that originated on a node other than a currentlypreferred or home node), the method may include passing ownership of thelock to a thread on a remote list for the LIFON lock (e.g., one thatmaintains data representing threads that originated from a non-preferred(or remote) node and that are waiting for an opportunity to contend forthe LIFON lock. This is illustrated in FIG. 9 by the positive exit of915 and element 920. In various embodiments, the thread on the remotelist to which ownership of the lock is passed may be selected using LIFOordering or using another mechanism. As illustrated in this example, insome embodiments the method may also include changing which node isconsidered the preferred node (e.g., making the node from which theselected thread originated the preferred node) before returning, as in925.

In this example, if the applicable fairness mechanism does not (at leastat this time) favor a remote thread (shown as the negative exit from915), and if both the main stack and remote lists are empty (shown asthe positive exits from 930 and 935), the method may include releasingthe LIFON lock and then returning, as in 940. However, if the applicablefairness mechanism does not (at least at this time) favor a remotethread (shown as the negative exit from 915), and the main stack empty(shown as the positive exit from 930), but the remote list is not empty(shown as the negative exit from 935), the method may include thereleasing thread passing ownership of the LIFON lock to the head of theremote list and then returning, as in 945.

As illustrated in this example, if the main stack is not empty (shown asthe negative exit from 930) and if an applicable fairness mechanism(e.g., according to a local fairness policy) favors the tail of stack(shown as the positive exit from 950), the method may include thereleasing thread passing ownership of the LIFON lock to the thread attail of the main stack and then returning, as in 955. However, if themain stack is not empty (shown as the negative exit from 930) and if thefairness mechanism does not (at least at this time) favor the tail ofthe stack (shown as the negative exit from 950), the method may includethe releasing thread popping the thread at the head of the main stackfrom the stack and deciding (e.g., according to an applicable cullingpolicy) whether to cull this thread or grant it ownership of the lock(as in 960).

If the releasing thread decides not to cull the thread that was poppedfrom the main stack (at least at this time), the method may include thereleasing thread passing ownership of the LIFON lock to the thread thatwas popped from the head of the main stack and returning. This isillustrated in FIG. 9 by the negative exit from 970 and element 980. Ifthe releasing thread decides to cull the thread that was popped from themain stack, the method may include the releasing thread moving thethread that was popped from the main stack to the remote list. This isillustrated in FIG. 9 by the positive exit from 970 and 975. Asillustrated in this example, the method may include repeating theoperations shown at 960 (e.g., popping another thread from the mainstack and deciding whether to cull the popped thread or grant ownershipof the LIFON lock to the popped thread) one or more times until the lockis passed to another thread, until the remote list and/or the main stackis empty, or until some other condition or event triggers a release ofthe LIFON lock and a return from the lock release operation withouthaving passed ownership of the LIFON lock to another thread. Note that,in other embodiments, following the negative exit of 975, the method mayinclude repeating the operations illustrated in FIG. 9 beginning atelement 915 or 930.

One embodiment of a LIFON lock that provides concurrency restriction isillustrated by the example pseudo-code below. As was the case with theLIFO lock described earlier, the lock state of a LIFON lock may beeither 0 or 1, or may point to the head of a stack of threads that arewaiting to acquire the LIFON lock (the main stack). As illustrated inFIG. 8 and described above, the LIFON lock includes. In addition, ratherthan the LIFON lock including a data structure whose data explicitlyrepresents an active circulation set on the LIFON lock, the threads atthe front of the main stack may be considered the active circulationset, and the deeper a thread is in the stack (the farther it is from thehead of the main stack) the less likely it is that the thread will beable to acquire the LIFON lock. Like the LIFO lock described above, theLIFON lock may be very unfair over the short term, but may be fair overthe longer term.

As noted above, and illustrated by the example LIFON pseudo-code below,all editing of the main stack may occur at unlock time within theLockRelease( ) operation, which may include (among other options) any ofthe following triggers: A) If the main stack is empty but the Remotelist is populated, there may be a deficit and the main stack may need tobe re-provisioned from the Remote list. This may enforce thework-conserving property of the LIFON lock. B) The top-of-stack pointeridentifies the candidate successor. If the thread identified by thetop-of-stack pointer is a remote thread, that element may be excisedfrom the main stack and relocated to the tail of the Remote list. Thatis, it may be culled from the main stack. C) Periodically, the thread atthe tail of main stack may be extracted and designated as the successor.In some embodiments, this anti-starvation mechanism may be used toimpose long-term intra-node local fairness. D) Periodically, the threadat the head of the remote list may be extracted and designated as thesuccessor, and the value of the indicator of the currently preferred(home) node, LHome, may be updated accordingly. In some embodiments,this anti-starvation mechanism may be used to impose long-terminter-node fairness. In some embodiments, rather than maintaining asingle unified list of all threads from remote nodes, a LIFON lock mayinclude multiple stacks (e.g., one for each node), and the long-termfairness policy may cause the preferred (home) node to switch betweenthem (e.g., in a round-robin fashion or using another fairnessmechanism). In some embodiments, the LIFON lock may maintain multiplenode-specific remote lists. For example, in a four-node system, a remotelist that is specific to node 0 (used when node 0 is the preferred/homenode) would include threads from nodes 1-3, a remote list that isspecific to node 1 (used when node 1 is the preferred/home node) wouldinclude threads from nodes 0, 2, and 3, and so on.

In the example LIFON pseudo-code shown below, the operationBernoulli(Self,P) may represent a Bernoulli trial implemented via athread-local uniform pseudo-random number generator. In variousembodiments, Bernoulli( ) may return true if the next random value is<P. In this example, the “Thread” structure includes Next, Grant andNodeID fields for use by the lock subsystem. Note that, even though theprevailing admission order for the LIFON lock is LIFO ordering, byminimizing the ACS size a round-robin cyclic admission order schedulemay be enforced. For example, if there is only one waiting member of theACS at unlock( ) time, then there is only one possible choice for asuccessor.

001: Class LIFON : 002:  Thread * Stack ; 003:  Thread * Remote ;   //list of Excess remote threads 004:  int LHome ;    // preferred (home)NUMA node 005:  int Epoch ; 006: 007: LockAcquire (Thread * Self,LIFON * L) : 008:  auto w = L->Stack 009:  Retry: 010:  if w == 0 : 011: // uncontended lock release path ... 012:  w = CASP (&L->Stack, 0, 1) ;013:  if w == 0 : return ; 014: 015:  ASSERT w != 0 ; 016:  Self->Grant= 0 ; 017:  Self->NodeID = CurrentNode( ) ; 018:  Self->Next = w ; 019: intptr_t v = CASP (&L->Stack, w, Self) ; 020: 021:  if v != w : 022: // CAS failed 023:  w = v ; goto Retry ; 024: 025:  // Waiting phase026:  while Self->Grant == 0 : 027:  Park ( ) ; 028:  ASSERT L->Stack !=0 ; 029:  ASSERT L->Stack != Self ; 030: } 031: 032: static voidLockRelease (Thread * Self, LockType * L) : 033:  // Periodically : 034: // Anti-starvation - impose long-tern inter-node fairness 035:  // popelement from Remote list 036:  // Pass ownership of lock to that element037:  // Change LHome accordingly 038:  // Over time, the Remote list isexpected to self-sort 039:  if L->Remote != NULL && Bernoulli (Self,RPrb) : 040:  ASSERT L->Stack != 0 ; 041:  Thread * r = L->Remote ; 042: L->Remote = r->Next ; 043:  L->LHome = r->NodeID ;   // changepreferred NUMA node 044:  L->Epoch ++ ; 045:  ASSERT r->Grant == 0 ;r->Grant = 1 ; Unpark(r) ; 046:  return ; 047: 048:  Retry : 049:  autow = L->Stack ; 050:  if w == 1 : 051:  // Deficit on main stack ; try tore-provision from Remote list 052:  // enforce work-conserving property053:  // If main stack is empty, then revert to head of remote list 054: Thread * r = L->Remote ; 055:  if r != NULL : 056:   L->Remote =r->Next ; 057:   ASSERT r->Grant == 0 ; r->Grant = 1 ; Unpark(r) ; 058:  return ; 059: 060:  // normal classic uncontended unlock 061:  // Bothmain stack and remote list are empty 062:  w = CASP (&L->Stack, 1, 0) ;063:  if w == 1 : return ; 064: 065:  ASSERT w != 0 && w != 1 ; 066:067:  // Impose long-term intra-node local fairness - anti-starvation068:  // occasionally extract tail of stack as successor 069:  // thehead of the list is volatile and vulnerable to concurrent modification070:  // but the interior of the list is stable while the lock remainsheld. 071:  // Try to pop from tail : 072:  if Bernoulli (Self, TailPrb): 073:  // remove tail from list and grant ownership to tail 074:  autoTail = PopTail (m->Stack) ; 075:  if Tail != NULL : 076:   ASSERTTail->Grant == 0 ; Tail->Grant = 1 ; Unpark(Tail) ; 077:   return ; 078:079:  // There is at least one thread on the stack 080:  // Whilelocked, the stack is grow-only : push-only 081:  TryPop : 082:  Thread *n = w->Next ; 083:  ASSERT n != NULL ; 084:  ASSERT n != w ; 085: 086: auto v = CASP (&L->Stack, w, n) ; 087:  if v == w : 088:  // CAS wassuccessful ; w has been successfully popped from the top-of-stack 089: // w is now detached 090:  // Decide whether to pass ownership to w orcull it the Remote list. 091: 092:  if w->NodeID != L->LHome : 093:   //w is remote! 094:   // Avoid futile culling ... 095:   // Cull “w” onlyif there are potentially better candidates on either the main 096:   //stack or on the Remote list. 097:   // There is no point in culling “w”if both the main stack is empty and if all 098:   // members of theRemote list are known with certainty to be remote. 099:   // Head->Epoch== L->Epoch implies that all members of the Remote list are 100:   //definitely remote. 101:   // If “w” were to be culled naively when n ==1 and (Head == null or 102:   // Head->Epoch == L->Epoch) then theadmission schedule would devolve to 103:   // round-robin, failing tobenefit from concurrency restriction. 104:   Thread * Head = L->Remote ;105:   if n != 1 || (Head != NULL && Head->Epoch != L->Epoch) : 106:  // Cull remote element from main stack to Remote list 107:   w->Epoch= L->Epoch ; 108:   Append w to L->Remote list 109:   goto Retry ;   //transfer remote prefix 110: 111:  ASSERT w->Grant == 0 ; w->Grant = 1 ;unpark(w) ; 112:  return ; 113: 114:  // CAS failed; thread raced andlost 115:  // inopportune interleaving -- concurrent interference --race 116:  // some other thread modified L->Stack in the LD-CAS windowabove 117:  // new threads have arrived in interim window 118:  w = v ;119: goto TryPop ; 120: 121:

In the example pseudo-code above, the call to the Bernoulli operation(at line 39) may implement a randomization similar to a biased cointoss, in which the second parameter (RPrb) represents the probabilitythat the operation will return true. In some embodiments, this parametermay be set so that this probability is very low (e.g., so that theoperation returns true for only about 1 out of every 1000 trials). Inthis example, at line 50, if it is determined that the main stack hasrun out of nodes, it may need to be re-provisioned. Although takingthreads from the remote list may not be preferable, one of the threadson the remote list may be moved to the main stack if there are no betteroptions.

In this example, the policy that is implemented for imposing intra-nodelong-term fairness (one that designates the tail of the main stack,rather than the head of the main stack, as the successor) is describedin lines 67-72. In this example, the variable at line 92 identifies thecurrent “preferred home node”, which is the NUMA node being served atthe moment. As described in the example pseudo-code above, a thread maybe pulled off the local (main) stack, but the LIFON mechanisms may havechanged which NUMA node is the preferred (home) node recently. In thiscase, the thread that was pulled off the local stack may actually beconsidered to be a remote thread, and may be transferred from the mainstack to the remote list.

In some systems, by making the set of changes to the LIFO lock describedherein, a LIFO lock that is deeply unfair and NUMA-oblivious may betransformed into a lock that is unfair in the short term for the threadsin a given node (the preferred/home node), but is fair over the longterm for these local threads (due to the occasional selection of thetail node) and that is also NUMA-aware. As described herein, theresulting LIFON lock may attempt to keep ownership on a given node(e.g., by keeping the remote threads sequestered away on a separateremote list), but may apply long-term fairness policies such as thosedescribed herein. This new LIFON lock has, in various experiments, beenfound to work quite well for a variety of multithreaded applications.

In some embodiments, an MCS type lock may be modified to provideconcurrency restriction by adding an explicit list of passive or “cold”threads. In such embodiments if, at unlock( ) time, there exist anyintermediate nodes in the queue between the lock owner's node and thecurrent tail, there are surplus threads in the ACS. In this case, one ofthe surplus threads may be unlinked and excised, and then transferred tothe passive list on which “cold” threads reside. Conversely, if, atunlock( ) time, the main queue is empty except for the owner's node, anode may be extracted from the passive list and inserted into the queueat the tail of the queue, and then ownership may be passed to thatthread. In some embodiments, a concurrency-restricting variant of an MCStype lock may directly edit the MCS chain to shift threads back andforth between the main chain and the explicit list of passivatedthreads. One embodiment of a concurrency-restricting variant of an MCStype lock (referred to herein as an MCSCR lock) is illustrated by theexample pseudo-code below.

In this example, the MCSCR lock is NUMA-oblivious (which may beperfectly suitable in certain circumstances, such as in a non-NUMAsystem). Note that a NUMA-aware variant of this lock (referred to hereinas an MCSCRN lock) is illustrated by the example pseudo-code in asubsequent listing. The example MCSCR lock illustrated below attempts torestrict the number of threads that are circulating, without regard tothe demographic makeup of the threads that are circulating. Here, incontrast with a standard MCS lock, the release method includes a call toa Bernoulli operation (at line 58) whose return value is used to decidewhen (and whether) to apply a long-term fairness policy to the selectionof the successor. In this example, in addition to the modified MCS lock,the MCSCR lock includes an excess list, which may be similar to theremote list of the previous example, except that the threads on the listdo not have to be remote. Instead, the threads on the excess list needonly be threads that do not need to be in circulation in order to keepthe lock saturated.

As illustrated in the example pseudo-code below, in various embodiments,an MCSCR lock may provide succession by direct handoff. In this example,the “Excess” list (which is a singly-linked null-terminated list)contains the passive set and is protected by the main lock itself, suchthat only the current owner can access the Excess list. As with otherconcurrency-restricting locks described herein, all editing of the mainMCS chain may occur at unlock time within the LockRelease( ) operation,which may include (among other options) any of the following triggers:A) If there is a deficit in the main list, an attempt may be made tore-provision the main list from the head of the Excess list. Thispreserves the work-conserving property of the lock. B) If there aresurplus threads in the main MCS chain, threads may be culled andexcised, and then moved to the tail of excess list. The “surplus”condition may be detected if there are three or more threads on the mainMCS chain. For example, one of the nodes on the chain is associated withthe current owner. If there is a second element in the chain, thenownership will be granted to that thread when the owner releases thelock. If there are three or more elements in the chain, there is anexcess of threads, and a culling operation may be performed in order toprovide concurrency restriction. C) Periodically, to impose long-termintra-node fairness the head of the Excess list may be popped andinserted into the main MCS chain. That is, an element may be migratedfrom the passive set to the active circulation set. Subsequently, someother member of the active circulation set will be detected as surplusand displaced to the Excess list.

In some embodiments, this approach may be extended to provideconcurrency restricting by using thread ordinal IDs (0, 1, 2, etc.) aspriority indicators, and periodically shuffling the ordinals, ordefining the effective priority of a thread as follows:EffectivePriority=(AssignedOrdinal+RRBasis) mod MaxPriority, whereAssignedOrdinal is a thread-local variable that reflects the thread'sassigned unique order identification number. In such an embodiment, toimpose long-term fairness, the RRBasis (which may be a global variable)may be peridocially advanced.

One embodiment of an MCSCR lock is illustrated in the examplepseudo-code below.

001: // Classic MCS “QNode” 002: // The QAlloc( ) and QFree( ) operatorsallocate and free QNode instances. 003: // The SWAP( ) operatoratomically loads the value from the address given 004: // as the 1stargument and stores the 2nd argument into that address, and 005: // thenreturns the original value. 006: 007: Class QNode : 008:  Thread * Assoc009:  volatile int Event 010:  QNode * Next 011: 012: Class MCSCRLock :013:  QNode * Tail  // tail of MCS chain : threads enqueue here 014: QNode * Owner  // head of MCS chain : current owner 015:  QNode *Excess  // PS 016: 017: // Pass and grant ownership to thread associatedwith “succ” 018: // wake thread -- succession by direct handoff 019:020: Resume (QNode * succ) : 021:  Thread * up = succ->Assoc ; 022: ASSERT succ->Event == 0 ; 023:  succ->Event = 1 ; 024:  Unpark(up) ;025: } 026: 027: // LockAcquire( ) : almost unchanged from baseline MCS028: 029: LockAcquire (Thread * Self, MCSCRLock * m) : 030:  QNode * n =QAlloc (Self) ; 031:  n->Assoc = Self ; 032:  n->Next = NULL ; 033: n->Event = 0 ; 034:  QNode * prv = SWAPP (&m->Tail, n) 035:  if pry !=NULL : 036:   prv->Next = n ; 037:   while n->Event == 0 : 038:    Park( ) ; 039: 040:  // Record the current owner node : memorize forsubsequent unlock 041:  m->Owner = n ; 042: 043: LockRelease (Thread *Self, MCSCRLock * m) : 044:  ASSERT m->Tail != NULL ; 045:  QNode * n =m->Owner ; 046:  ASSERT n != NULL ; 047:  QNode * succ = n->Next ; 048:049:  // Periodically move and element from Excess list into main MCSchain 050:  // Impose long-term fairness ; anti-starvation 051:  //Force circulation between ACS and Excess 052:  // Injecting anadditional thread into ACS will usually cause and result in 053:  //subsequent culling of excess threads from the ACS. 054:  // Bernoulli( )is a Bernoulli trial implemented via a thread-local uniform 055:  //pseudo-random number generator. 056:  // Bernoulli( ) returns true ifthe next random value is < PE. 057:  QNode * k = m->Excess ; 058:  if k!= NULL && Bernoulli (Self, PE) : 059:   // Extract element fromm->Excess that has waited the longest 060:   k = PopTailElement(&m->Excess) ; 061: 062:   // Now insert k into main MCS chain 063:  ASSERT k->Event == 0 ; 064:   // insert k at tail 065:   // Thefollowing is analogous to arrival code in LockAcquire( ) 066:   //Specialized variant - it is known that the chain is populated with at067:   // least one element : k 068:   k->Next = NULL ; 069:   QNode *prv = SWAP (&m->Tail, k) ; 070:   ASSERT prv != NULL ; 071:   prv->Next= k ; 072:   for : 073:    succ = n->Next ; 074:    if succ != NULL :break ; 075:    Pause( ) ; 076:   ASSERT succ != NULL ; 077:   Resume(succ) ; 078:   QFree (Self, n) ; 079:   return ; 080: 081:  if succ ==NULL : 082:   // No apparent visible successors on main MCS chain 083:  // there is a deficit : move from Excess list to ACS - re-provision084:   // Critical property : work conserving 085:   QNode * k =m->Excess ; 086:   if k != NULL : 087:    // pop from Head of ExcessList : most recently arrived thread 088:    // most likely to still bespinning in STP waiting 089:    // anticipate and prepare for insertionat m->Tail 090:    m->Excess = k->Next ; 091:    k->Next = NULL ; 092:   ASSERT k->Event == 0 ; 093:    if CASP (&m->Tail, n, k) == n : 094:    // Success 095:     Resume (k) ; 096:     QFree (Self, n) ; 097:    return 098:    } 099:    // CAS failed - inopportune interleaving100:    // this thread raced and lost ; some other thread updatedm->Tail in window 101:    // recent arrival updated m->Tail 102:    //restore K to Excess 103:    k->Next = m->Excess ; 104:    m->Excess = k; 105: 106:   if CASP (&m->Tail, n, NULL) == n : 107:    QFree (Self, n); 108:    return 109: 110:   // rarely taken path : latent-tardy storeinto n->Next 111:   // Wait for chain to resolve 112:   for : 113:   succ = n->Next ; 114:    if succ != NULL break ; 115:    Pause( ) ;116: 117: 118:  ASSERT succ != NULL ; 119:  ASSERT n->Next == succ ;120:  ASSERT succ != n ; 121:  ASSERT succ->Event == 0 ; 122:  ASSERTm->Tail != n ; 123: 124:  // Cull excess threads from ACS into Excesslist 125:  // MOVE FROM Active-Circulating set [ACS] to Excess ; cullexcess 126:  // Interior of MCS chain is stable for owner 127:  // ss !=null implies excess-surplus ; main list is overprovisioned 128:  //Extract victim and move from MCS chain to head of Excess ; push 129:130:  Grind : (0) ; 131:  QNode * ss = succ->Next ; 132:  if ss != NULL: 133:   ASSERT ss->Event == 0 ; 134:   ASSERT m->Tail != succ ; 135:  // Have excess threads on main MCS chain ; more than needed forsaturation 136:   // Succ is surplus - excess ; not needed to saturatelock 137:   // Splice out succ from main MCS chain ; excise and cull138:   // Succ is in the interior and is NOT at the tail 139:   n->Next= ss ; 140: 141:   // Move succ onto Excess list 142:   // Prepend succto Excess : push ; LIFO order 143:   succ->Next = m->Excess ; 144:  m->Excess = succ ; 145: 146:   // Designate ss as successor ; replacesucc 147:   // Pass lock to ss intead of succ 148:   succ = ss ; 149:150:   // the operation can cull incrementally or “grind” away acomplete remote 151:   // prefix of threads from the MCS chain. 152:  // Either approach is viable. 153:   goto Grind ; 154: 155: ResumeSucc : (0) ; 156:  Resume (succ) ; 157:  QFree (Self, n) ; 158: return ; 159:

In the example pseudo-code above, the fairness policy is applied at line58 where, every once in a while, a thread is taken out of the excesslist (which represents the passive set for this concurrency-restrictinglock) and is put back onto the MCS chain (which represents the activecirculation set for this lock). Without this operation, the lock wouldbe unfair and could lead to indefinite waiting for at least somethreads. In this example, at line 86, the thread may decide that themain MCS queue appears to be empty. Absent an excess list, this would bethe point at which a standard MCS lock might transition from the lockedstate to the unlocked state, because there would be no thread holdingthe lock and no other threads waiting for the lock. However in theexample illustrated above, at the point at which it looks like the lockis going to go unlocked, there may be threads waiting on the excesslist. In this case, one of the waiting threads may be pulled off theexcess list and ownership of the lock may be passed to that thread.

In the example pseudo-code above, there is a culling operation at line131. Note: in the MCS chain, there is a node called n that correspondsto the lock owner that is dropping the lock, there is a node that isdesignated as the successor (succ), and there is a node that isdesignated as the successor of the successor (ss). In this example, thechain of lock ownership (the MCS chain) goes from n (owner), to succ, toss. In this example, if there is a node designated as ss, the lock isover-provisioned, i.e., there are more than enough threads on that mainchain than are needed to saturate the lock. At that point, a cullingoperation may be performed. In this example, the culling operation maybe configured to extract the middle thread (the thread that wasdesignated as succ) out of the chain and move it to the excess list,this reducing the active circulation set for the lock.

In some embodiments, the MCSCR lock described above may be madeNUMA-friendly by intentionally checking (at unlock( ) time) to see ifthe next thread on the main MCS list of waiting threads resides on thecurrently preferred NUMA node. If not, the lock implementation mayintentionally unlink and sequester that next node, moving it from themain MCS chain to the passive list. Relatedly, when picking threads fromthe excess list (i.e., the passive set or “cold” list), there may be apreference for threads that reside on the currently preferred NUMA node.In this example, the lock implementation may attempt to keep the threadsthat are nearby (e.g., those executing on the preferred/home node)toward the front of the excess list and the threads that are fartheraway (those that are executing on other nodes) toward the tail of thelist. Therefore, if there is a need to re-provision the main MCS chain,the threads pulled off the head of the excess list are more likely to belocal threads. and the threads that are remote are more likely to befound toward the tail of the excess list

In some embodiments, in order to further augment the approach to beNUMA-friendly, the lock implementation may compute a priority as:EffectivePriority=(Node+RRBias) mod NNodes, where RRBias identifies thecurrently preferred (home) NUMA node, NNodes is the number of NUMA nodesin the system, and Node is a thread-local variable that identifies thecurrent node on which the thread is running.

One embodiment of a MCSCRN lock is illustrated by the examplepseudo-code below. In this example, the MCSCRN lock is a NUMA-friendlyadaptation of the MCSCR lock described earlier. The lock specifies thecurrently preferred NUMA node in its “LHome” field, and the admissionpolicies attempt to restrict the ACS to threads from that node. Here, athread may be considered “local” with respect to the lock if the threadruns on the currently preferred node. Otherwise the thread may beconsidered remote. As in the previous example, the “Excess” listrepresents the passive set for the MCSCRN lock. As described above, thehead (prefix) of the Excess list tends to contain local threads thathave been passivated, and the tail (suffix) tends to contain remotepassive thread. Conceptually there may be two distinct lists,ExcessLocal and ExcessRemote, but a single unified list may be moreconvenient for the implementation, in some embodiments.

As with other concurrency-restricting locks described herein, allediting of the main MCS chain may occur at unlock time within theLockRelease( ) operation, which may include (among other options) any ofthe following triggers: A) If there is a deficit in the main list, anattempt may be made to re-provision the main list from the head of theExcess list. This preserves the work-conserving property of the lock. B)If there are surplus threads in the main MCS chain, threads may beculled and excised. The “surplus” condition may be detected if there arethree or more threads on the main MCS chain. For example, one of thenodes on the chain is associated with the current owner. If there is asecond element in the chain, then ownership will be granted to thatthread when the owner releases the lock. If there are three or moreelements in the chain, there is an excess of threads, and a cullingoperation may be performed in order to provide concurrency restriction.If the surplus culled thread is local, it may be prepended at the headthe Excess list, otherwise it may be appended at the tail of the Excesslist. C) Periodically, to impose long-term intra-node fairness the headof the Excess list may be popped and inserted into the main MCS chain.That is, an element may be migrated from the passive set to the activecirculation set. Subsequently, some other member of the activecirculation set will be detected as surplus and displaced to the Excesslist. D) If there is a successor on the main MCS chain and the successoris remote, there are either additional threads on the chain or theexcess list is populated. In this case, the successor may be culled tothe tail of the Excess list. In this case, the succession policy mayprefer a thread from the front of the Excess list or on the main MCSchain over a known remote element. E) Periodically, to impose long-terminter-node fairness, an operation to identify a successor may scanforward through the Excess list, skipping over local elements (threads)until the end of the list is reached or until a non-local element isencountered. That local prefix may then be detached and appended to theend of the Excess list. Finally, the first non-local element may beextracted and ownership of the lock may be passed to it, after whichLHome may be updated to reflect that the NUMA node on which the newowner is executing. In this manner, the ACS may subsequently self-sortin an incremental and gradual fashion. After a number of LockRelease( )calls and culling operations (in response to the changed LHome value),the ACS will tend toward homogeneity (e.g., to be composed of localelements).

001: Class QNode : 002:  Thread * Assoc 003:  volatile int Event 004: QNode * Next 005:  int NodeID 006: 007: Class MCSCRN : 008:  QNode *Tail 009:  QNode * Owner 010:  QNode * Excess 011:  int LHome //currently preferred NUMA node number 012: 013: LockAcquire (Thread *Self, MCSCRN * m) : 014:  QNode * n = QAlloc (Self) ; 015:  n->Assoc =Self ; 016:  n->NodeID = CurrentNode( ) ; 017:  n->Next = NULL ; 018: n->Event = 0 ; 019:  QNode * prv = SWAP (&m->Tail, n) ; 020:  if prv !=NULL : 021:  prv->Next = n ; 022:  while n->Event == 0 : 023:   Park( )024: 025:  // Record the current owner node : memorize for subsequentunlock 026:  // Pass “n” from lock to unlock 027:  m->Owner = n ; 028: return 029: 030: LockRelease (Thread * Self, MCSCRN * m) : 031:  ASSERTm->Tail != NULL ; 032:  QNode * n = m->Owner ; 033:  ASSERT n != NULL ;034:  top : (0) ; 035: 036:  // Periodically ... 037:  // imposelong-term Local intra-node fairness ; anti-starvation 038:  // Move headof Excess into main chain and ACS 039:  // 040:  // Force circulationbetween ACS and Excess 041:  // Injecting an additional thread into ACSwill _usually cause and result in 042:  // subsequent culling of excessthreads from the ACS. 043:  QNode * k = m->Excess ; 044:  ASSERT n != k&& k != m->Tail ; 045:  if (k != NULL && k->NodeID == m->LHome &&Bernoulli (Self, PE) : 046:  // pop k from head of excess 047: m->Excess = k->Next ; 048: 049:  // insert k into main MCS chain 050: QNode * succ = n->Next ; 051:  ASSERT (succ != n) ; 052:  // append :insert k to tail of MCS chain 053:  // The following is analogous toarrival code in LockAcquire( ) 054:  // Specialized variant 055:  // itis known that the chain is populated with at least one element : k 056: // Beware that prv might equal n 057:  k->Next = NULL ; 058:  QNode *prv = SWAP (&m->Tail, k) ; 059:  ASSERT prv != NULL && prv != n && prv!= k ; 060:  prv->Next = k ; 061:  for : 062:   succ = m->Next 063:   ifsucc != NULL : break 064:   Pause 065:  Resume (succ) ; 066:  QFree(Self, n) ; 067:  return ; 068: 069:  // Periodically ... 070:  //Impose long-term inter-node NUMA fairness 071:  // scan forward throughexcess until null or 1st remote element 072:  // Rotate local prefixfrom head to tail of Excess 073:  if k != NULL && Bernoulli (Self, PL) :074:  QNode * LocalPrefix = NULL ; 075:  for : 076:   k = m->Excess ;077:   if (k == NULL : 078:   // No remote elements found on excess List079:   // Just leave Excess unchanged 080:   m->Excess = LocalPrefix ;081:   break ; 082: 083:   int nn = k->NodeID ; 084:   if nn == m->LHome: 085:   m->Excess = k->Next ; 086:   LocalPrefix = AppendTo(LocalPrefix, k) ; 087:   continue ; 088: 089:   // encountered 1stremote element 090:   // Select new-next preferred NUMA node 091:  m->LHome = nn ; 092:   m->Excess = Concatenate (m->Excess,LocalPrefix) 093:   break ; 094:  // fall thru 095: 096: 097:  QNode *succ = n->Next ; 098:  ASSERT succ != n ; 099:  if succ == NULL : 100: // No apparent visible successors on main MCS chain 101:  // Deficit inmain MCS list : re-provision on-demand from Excess 102:  // Move elementfrom head of Excess list to MCS chain 103:  // Critical property : workconserving 104:  if k != NULL : 105:   // completely extract k fromExcess list 106:   // anticipate and prepare for insertion at m->Tail107:   ASSERT k->Event == 0 ; 108:   m->Excess = k->Next ; 109:  k->Next = NULL ; 110:   if CASP (&m->Tail, n, k) == n : 111:   //Success ! 112:   Resume(k) ; 113:   QFree (Self, n) ; 114:   return ;115: 116:   // CAS failed; inopportune interleaving 117:   // thisthread raced and lost; Some other thread updated m->Tail in window 118:  // recent arrival updated m->Tail 119:   // return K to Excess --repair and undo removal 120:   // return; restore; repair; reinstate;121:   k->Next = m->Excess ; 122:   m->Excess = k ; 123: 124:  //Classic normal uncontended locking path ... 125:  if CASP (&m->Tail, n,NULL) == n : 126:   QFree (Self, n) ; 127:   return ; 128: 129:  //rarely taken path : latent-tardy store into n->Next 130:  // Wait forchain to resolve 131:  for : 132:  succ = n->Next ; 133:  if succ !=NULL break ; 134:  Pause( ) ; 135: 136:  ASSERT succ != NULL ; 137: ASSERT n->Next == succ ; 138:  ASSERT succ != n ; 139:  ASSERTsucc->Event == 0 ; 140:  ASSERT m->Tail != n ; 141: 142:  // Cull excessthread from main MCS chain into Excess list 143:  // Interior of MCSqueue is stable for owner 144:  // Check for excess threads/QNodes onthe main list. 145:  // ss != null implies excess : main MCS listoverprovisioned, at least transiently. 146:  // Identify victim and movefrom MCS queue to Excess 147:  // 148:  // remark : culling also helpsto re-order ACS to reduce ACS intra-cycle 149:  // NUMA nodetransitions, which in turn reduces lock migration rates. 150: 151: Grind : (0) ; 152:  QNode * ss = succ->Next ; 153:  if ss != NULL :154:  // Have excess threads on main MCS chain ; more than needed forsaturation 155:  // Splice out succ from main MCS chain; excise and cull156:  // Succ is in the interior and is NOT at the tail 157:  // Succ issurplus - excess ; not needed to saturate lock 158:  n->Next = ss ; 159: if succ->NodeID != m->LHome : 160:   // Remote -- Add succ to Excess :append 161:   m->Excess = AppendTo (m->Excess, succ) ; 162:  else 163:  // Local -- Add succ to Excess : prepend for stack LIFO-like admissionorder 164:   succ->Next = m->Excess ; 165:   m->Excess = succ ; 166:167:  // Designate ss as successor; replace succ 168:  // Pass lock toss instead of succ 169:  succ = ss ; 170:  goto Grind ; 171: 172:  ifsucc->NodeID != m->LHome && m->Excess != NULL : 173:  // Successor isremote and alternative successor exists on Excess list 174:  //Potentially better alternatives exist 175:  // Prefer unknown or localover known remote. 176:  // Try to improve both ACS demographics andorder 177:  // Filter and edit ACS to reduce NUMA diversity 178:  // popK from Excess list 179:  k = m->Excess ; 180:  m->Excess = k->Next ;181:  ASSERT k != n ; 182: 183:  // append k on MCS chain at tail 184: k->Next = NULL ; 185:  QNode * prv = SWAPP (&m->Tail, k) ; 186:  ASSERTprv != NULL && prv->Next == NULL && prv != k && prv != n ; 187: prv->Next = k ; 188: 189:  // wait for succ->Next to resolve 190:  //it is known it will resolve because k was just added above 191:  QNode *ss 192:  for : 193:   ss = succ->Next 194:   if ss != NULL : break 195:  Pause( ) 196: 197:  // excise succ from main MCS chain 198:  n->Next =ss ; 199: 200:  // append succ to Excess List 201:  m->Excess = AppendTo(m->Excess, succ) ; 202: 203:  // Designate ss as successor; replacesucc 204:  // Pass lock to ss instead of succ 205:  succ = ss ; 206:207:  Resume (succ) ; 208:  QFree (Self, n) ; 209:  return ; 210:

Each of the example concurrency-restricting lock algorithms describedherein has been implemented within a virtual machine environment and/ora transaction or concurrency support library. The performanceimprovements over previously best performing locks were oftensignificant. In general, the concurrency-restricting approachesdescribed herein may be applied to conserve (or reduce contention on)any resource that is shared by multiple threads and that may exhibit akind of thrashing capability under heavy contention. For example, invarious embodiments, the shared resources that can be conserved by theseconcurrency restriction techniques may include any or all of thefollowing:

-   -   cache residency and/or DRAM channel access    -   thermal and/or energy headroom    -   pipelines    -   logical CPU occupancy    -   NUMA interconnect bandwidth (for NUMA-aware concurrency        restriction)    -   traditional memory pressure

Example System

FIG. 10 illustrates a computing system configured to implement some orall of the methods described herein for restricting concurrency oncontended locks, according to various embodiments. The computer system1000 may be any of various types of devices, including, but not limitedto, a personal computer system, desktop computer, laptop or notebookcomputer, mainframe computer system, handheld computer, workstation,network computer, a consumer device, application server, storage device,a peripheral device such as a switch, modem, router, etc, or in generalany type of computing device.

The mechanisms for implementing NUMA-aware cohort locking and/orNUMA-aware reader-writer locks, as described herein, may be provided asa computer program product, or software, that may include anon-transitory, computer-readable storage medium having stored thereoninstructions, which may be used to program a computer system (or otherelectronic devices) to perform a process according to variousembodiments. A computer-readable storage medium may include anymechanism for storing information in a form (e.g., software, processingapplication) readable by a machine (e.g., a computer). Themachine-readable storage medium may include, but is not limited to,magnetic storage medium (e.g., floppy diskette); optical storage medium(e.g., CD-ROM); magneto-optical storage medium; read only memory (ROM);random access memory (RAM); erasable programmable memory (e.g., EPROMand EEPROM); flash memory; electrical, or other types of medium suitablefor storing program instructions. In addition, program instructions maybe communicated using optical, acoustical or other form of propagatedsignal (e.g., carrier waves, infrared signals, digital signals, etc.)

In various embodiments, computer system 1000 may include one or moreprocessors 1070; each may include multiple cores, any of which may besingle or multi-threaded. For example, as illustrated in FIG. 3,multiple processor cores may be included in a single processor chip(e.g., a single processor 1070 or processor chip 310), and multipleprocessor chips may be included on a CPU board (such as a CPU board300), two or more of which may be included in computer system 1000. Eachof the processors 1070 may include a hierarchy of caches, in variousembodiments. For example, as illustrated in FIG. 3, each processor chip310 may include multiple level 1 caches 330 (e.g., one per processorcore) and one or more other caches (which may be shared by the processorcores on the processor chip), such as level 2 caches 335. The computersystem 1000 may also include one or more persistent storage devices 1050(e.g. optical storage, magnetic storage, hard drive, tape drive, solidstate memory, etc) and one or more system memories 1010 (e.g., one ormore of cache, SRAM, DRAM, RDRAM, EDO RAM, DDR 10 RAM, SDRAM, RambusRAM, EEPROM, etc.). Various embodiments may include fewer or additionalcomponents not illustrated in FIG. 10 (e.g., video cards, audio cards,additional network interfaces, peripheral devices, a network interfacesuch as an ATM interface, an Ethernet interface, a Frame Relayinterface, etc.)

The one or more processors 1070, the storage device(s) 1050, and thesystem memory 1010 may be coupled to the system interconnect 1040. Oneor more of the system memories 1010 may contain program instructions1020. Program instructions 1020 may be executable to implement one ormore applications 1022 (which may include one or more accesses to acritical section of code or shared resource protected by a NUMA-awarecohort lock or a NUMA-aware reader-writer lock, as described herein),shared libraries 1024 (which may include a transaction support libraryand/or a concurrency support library), or operating systems 1026. Insome embodiment, program instructions 1020 may be executable toimplement a contention manager (not shown). Program instructions 1020may be encoded in platform native binary, any interpreted language suchas Java™ byte-code, or in any other language such as C/C++, Java™, etcor in any combination thereof. The program instructions 1020 may includefunctions, operations and/or other processes for implementing NUMA-awarecohort locking and/or NUMA-aware reader-writer locks, as describedherein. Such support and functions may exist in one or more of theshared libraries 1024, operating systems 1026, or applications

1.-20. (canceled)
 21. A method, comprising: performing by a computingdevice: beginning execution of a multithreaded application thatimplements multiple threads that access a critical section of code or ashared resource protected by an outer-inner dual path (OIL) lock,wherein the OIL lock includes an inner lock and an outer lock;responsive to a determination that a first thread has acquired the outerlock: permitting the first thread to access the critical section of codeor the shared resource; and placing the first thread in an activecirculation set, wherein threads in the active circulation set contendfor the outer lock; responsive to a determination that a second threadcannot acquire the outer lock: placing the second thread in a passiveset, wherein threads in the passive set contend for the inner lock;responsive to a determination that the second thread in the passive sethas acquired the inner lock: permitting the second thread to contend forthe outer lock.
 22. The method of claim 21, wherein: the determiningthat the second thread cannot acquire the outer lock comprises: causingthe second thread to spin for a spin period to wait for the outer lock,wherein the second thread is eligible to be scheduled for execution bythe computing device during the spin period; determining that the spinperiod as expired; and the placing the second thread in a passive setcomprises: causing the second thread to be parked, wherein the secondthread is not eligible to be scheduled for execution by the computingdevice while it is parked.
 23. The method of claim 21, wherein theacquiring of the inner lock by the second thread comprises: repeatedlychecking a status of the outer lock to determine whether the outer lockis available; responsive to a determination that the outer lock isavailable, attempting to acquire the outer lock.
 24. The method of claim21, further comprising: performing by a computing device: applying aculling policy to move at least one thread from the active circulationset to the passive set, wherein the culling policy is applied based atleast in part on a determined saturation level of the OIL lock.
 25. Themethod of claim 24, wherein: the computing device implements amultiprocessor system of multiple processor nodes employing anon-uniform memory access (NUMA) memory architecture, and applying theculling policy comprises determining, in the active circulation set, oneor more excess threads associated with one or more non-preferredprocessor nodes to be moved to the passive set.
 26. The method of claim21, further comprising: performing by a computing device: applying afairness policy to move at least one thread from the passive set to theactive circulation set, wherein the fairness policy is applied based atleast in part one or more of: an amount of time that a given thread aswaited in the passive set; a value of a counter; and a value returned bya randomization function.
 27. The method of claim 21, furthercomprising: subsequent to the accessing of the critical section of codeor the shared resource by the first thread: passing, by the firstthread, ownership of the OIL lock to another thread; and releasing, bythe first thread, the OIL lock, including both the outer lock and theinner lock.
 28. The method of claim 21, further comprising: subsequentto the accessing of the critical section of code or the shared resourceby the first thread: determining whether to apply a culling policy tomove one or more threads from the active circulation set to the passiveset; and determining whether to apply a fairness policy to move one ormore threads from the passive set to the active circulation set.
 29. Themethod of claim 21, further comprising: responsive to a determinationthat the second thread in the passive set has acquired the outer lock:permitting the second thread to access the critical section of code orthe shared resource; and moving the second thread to the activecirculation set.
 30. The method of claim 21, wherein the outer lock isimplemented as a test-and-test-and-set (TATAS) lock.
 31. The method ofclaim 21, wherein the inner lock is implemented as a Mellor-Crummey andScott (MCS) lock wherein threads contending for the MCS lock aremaintained in a linked list and spin on local variables.
 32. A systemcomprising: a computing device with one or more hardware processors withassociated memory, wherein the memory stores program instructions thatwhen executed on the one or more hardware processors cause the computingdevice to: execute a multithreaded application that implements multiplethreads that access a critical section of code or a shared resourceprotected by an outer-inner dual path (OIL) lock, wherein the OIL lockincludes an inner lock and an outer lock; responsive to a determinationthat a first thread has acquired the outer lock: permit the first threadto access the critical section of code or the shared resource; and placethe first thread in an active circulation set, wherein threads in theactive circulation set contend for the outer lock; responsive to adetermination that a second thread cannot acquire the outer lock: placethe second thread in a passive set, wherein threads in the passive setcontend for the inner lock; responsive to a determination that thesecond thread in the passive set has acquired the inner lock: permit thesecond thread to contend for the outer lock.
 33. The system of claim 32,wherein the program instructions when executed on the one or morehardware processors cause the computing device to: responsive to adetermination that the second thread in the passive set has acquired theouter lock: permit the second thread to access the critical section ofcode or the shared resource; and move the second thread to the activecirculation set.
 34. The system of claim 32, wherein the outer lock isimplemented as a test-and-test-and-set (TATAS) lock.
 35. The system ofclaim 32, wherein the inner lock is implemented as a Mellor-Crummey andScott (MCS) lock wherein threads contending for the MCS lock aremaintained in a linked list and spin on local variables.
 36. The systemof system 32, wherein: to determine that the second thread cannotacquire the outer lock, the program instructions when executed on theone or more hardware processors cause the computing device to: cause thesecond thread to spin for a spin period to wait for the outer lock,wherein the second thread is eligible to be scheduled for execution bythe computing device during the spin period; determine that the spinperiod as expired; and to place the second thread in a passive set, theprogram instructions when executed on the one or more hardwareprocessors cause the computing device to: cause the second thread to beparked, wherein the second thread is not eligible to be scheduled forexecution by the computing device while it is parked.
 37. The system ofclaim 32, wherein to acquire of the inner lock by the second thread, theprogram instructions when executed on the one or more hardwareprocessors cause the computing device to cause the second thread to:repeatedly check a status of the outer lock to determine whether theouter lock is available; responsive to a determination that the outerlock is available, attempt to acquire the outer lock.
 38. The system ofclaim 32, wherein the program instructions when executed on the one ormore hardware processors cause the computing device to: apply a cullingpolicy to move at least one thread from the active circulation set tothe passive set, wherein the culling policy is applied based at least inpart on a determined saturation level of the OIL lock.
 39. The system ofclaim 32, wherein the program instructions when executed on the one ormore hardware processors cause the computing device to: apply a fairnesspolicy to move at least one thread from the passive set to the activecirculation set, wherein the fairness policy is applied based at leastin part one or more of: an amount of time that a given thread as waitedin the passive set; a value of a counter; and a value returned by arandomization function.
 40. One or more non-transitory,computer-readable storage media storing program instructions that whenexecuted on or across one or more processors cause the one or moreprocessors to: execute a multithreaded application that implementsmultiple threads that access a critical section of code or a sharedresource protected by an outer-inner dual path (OIL) lock, wherein theOIL lock includes an inner lock and an outer lock; responsive to adetermination that a first thread has acquired the outer lock: permitthe first thread to access the critical section of code or the sharedresource; and place the first thread in an active circulation set,wherein threads in the active circulation set contend for the outerlock; responsive to a determination that a second thread cannot acquirethe outer lock: place the second thread in a passive set, whereinthreads in the passive set contend for the inner lock; responsive to adetermination that the second thread in the passive set has acquired theinner lock: permit the second thread to contend for the outer lock.